Last active
April 1, 2017 22:16
-
-
Save kazumalab/f889fcbcfe3c5c79510b518a2f2331a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
// Attach to Main Camera of CametaCenter Empty GameObject child. | |
public class CameraControl : MonoBehaviour { | |
private readonly float MAX_TURN_UP = 5f; | |
private readonly float MAX_TURN_DOWN = 0f; | |
private float InitPositionY = 0f; | |
public GameObject DummyCenter; // PlayerのCenter位置にdummyを置く | |
public GameObject TargetPlayer; // カメラが注目するPlayer | |
private Player TargetPlayerScript; | |
private float HorizonAngle = 0f; | |
private float VerticalAngle = 0f; | |
[SerializeField]private float radius = 1f; | |
private void Init () { | |
TargetPlayerScript = TargetPlayer.GetComponent<Player> (); | |
InitPositionY = transform.localPosition.y; | |
} | |
private void Awake () { | |
Init (); | |
} | |
private void Update () { | |
TrakingPlayer (); | |
HorizonAngle += Input.GetAxis ("Horizontal"); | |
VerticalAngle = Input.GetAxis ("Vertical"); | |
if (HorizonAngle > 0.5f || HorizonAngle < 0.5f) { | |
TurntoCenter (HorizonAngle, VerticalAngle); | |
} | |
if (Input.GetKey (KeyCode.Space)) { | |
RotateReset (); | |
} | |
} | |
// Function on Update. | |
public void TrakingPlayer () { | |
if (TargetPlayer != null) { | |
DummyCenter.transform.position = TargetPlayer.transform.position; | |
} | |
} | |
public void TurntoCenter (float dx, float heigh) { | |
float x = Mathf.Cos (dx); | |
float z = Mathf.Sin (dx); | |
print ("x : " + x + ", y : " + z); | |
transform.localPosition = Vector3.right * x * radius + Vector3.forward * z * radius + Vector3.up * transform.localPosition.y; | |
if(transform.localPosition.y < InitPositionY + MAX_TURN_UP && heigh > 0) | |
transform.localPosition += Vector3.up * heigh; | |
if(transform.localPosition.y > MAX_TURN_DOWN && heigh < 0) | |
transform.localPosition += Vector3.up * heigh; | |
if (transform.position.y >= TargetPlayerScript.HeightToRoof) | |
transform.position = Vector3.right * transform.position.x + Vector3.up * TargetPlayerScript.HeightToRoof + Vector3.forward * transform.position.z; | |
transform.LookAt (TargetPlayer.transform.position); | |
} | |
public void RotateReset () { | |
HorizonAngle = 0; | |
VerticalAngle = 0; | |
transform.localPosition = Vector3.one - Vector3.forward; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment