Created
October 9, 2017 00:37
-
-
Save urahimono/c570a06e7379a65e31966430610a14de to your computer and use it in GitHub Desktop.
【Unity】オブジェクトについてくるカメラを作ったよ
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
//------------------------------------------------------------------------ | |
// | |
// (C) Copyright 2017 Urahimono Project Inc. | |
// | |
//------------------------------------------------------------------------ | |
using UnityEngine; | |
public class CameraController : MonoBehaviour | |
{ | |
[SerializeField] | |
private Transform m_target = null; | |
[SerializeField] | |
private float m_speed = 0.0f; | |
[SerializeField] | |
private float m_waitRange = 0.0f; | |
public Transform Target | |
{ | |
get { return m_target; } | |
} | |
private Transform m_cameraTransform = null; | |
private Transform m_pivot = null; | |
private void Awake() | |
{ | |
SetCameraTransform(); | |
} | |
private void LateUpdate() | |
{ | |
UpdateCamera(); | |
} | |
public void Turn( float i_angle ) | |
{ | |
transform.rotation *= Quaternion.AngleAxis( i_angle, Vector3.up ); | |
} | |
private void UpdateCamera() | |
{ | |
if( Target == null ) | |
{ | |
return; | |
} | |
Vector3 toTargetVec = Target.position - transform.position; | |
float sqrLength = toTargetVec.sqrMagnitude; | |
// 設定した範囲内なら更新しない。 | |
// magnitudeはルート計算が重いので、二乗された値を利用しよう。 | |
if( sqrLength <= m_waitRange * m_waitRange ) | |
{ | |
return; | |
} | |
// ターゲットの位置から指定範囲内ギリギリの位置を目指すようにするよ。 | |
Vector3 targetPos = Target.position - toTargetVec.normalized * m_waitRange; | |
float deltaSpeed = m_speed * Time.deltaTime; | |
transform.position = Vector3.MoveTowards( transform.position, targetPos, deltaSpeed ); | |
} | |
[ContextMenu( "ApplyTarget" )] | |
private void ApplyForceTarget() | |
{ | |
if( Target == null ) | |
{ | |
return; | |
} | |
transform.position = Target.position; | |
SetCameraTransform(); | |
if( m_cameraTransform == null ) | |
{ | |
return; | |
} | |
m_cameraTransform.transform.LookAt( Target ); | |
} | |
private void SetCameraTransform() | |
{ | |
Camera camera = GetComponentInChildren<Camera>(); | |
Debug.AssertFormat( camera != null, "カメラが無ぇよ!" ); | |
if( camera == null ) | |
{ | |
return; | |
} | |
m_cameraTransform = camera.transform; | |
m_pivot = m_cameraTransform.parent; | |
} | |
} // class CameraController |
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
//------------------------------------------------------------------------ | |
// | |
// (C) Copyright 2017 Urahimono Project Inc. | |
// | |
//------------------------------------------------------------------------ | |
using UnityEngine; | |
[RequireComponent( typeof( Rigidbody ) )] | |
public class ObjectController : MonoBehaviour | |
{ | |
[SerializeField] | |
private float m_moveSpeed = 0.0f; | |
[SerializeField] | |
private float m_turnSpeed = 0.0f; | |
[SerializeField] | |
private float m_jumpForce = 0.0f; | |
[Header("Camera")] | |
[SerializeField] | |
private CameraController m_camera = null; | |
[SerializeField] | |
private float m_camraTurnSpeed = 0.0f; | |
private Rigidbody m_rigidbody = null; | |
private void Awake() | |
{ | |
m_rigidbody = GetComponent<Rigidbody>(); | |
} | |
private void Update() | |
{ | |
ControlCamera(); | |
ControlObject(); | |
} | |
private void ControlObject() | |
{ | |
Vector3 moveDir = Vector3.zero; | |
Vector3 forwardDir = m_camera.transform.forward; | |
Vector3 rightDir = m_camera.transform.right; | |
if( Input.GetKey( KeyCode.UpArrow ) ) | |
{ | |
moveDir += forwardDir; | |
} | |
if( Input.GetKey( KeyCode.DownArrow ) ) | |
{ | |
moveDir -= forwardDir; | |
} | |
if( Input.GetKey( KeyCode.RightArrow ) ) | |
{ | |
moveDir += rightDir; | |
} | |
if( Input.GetKey( KeyCode.LeftArrow ) ) | |
{ | |
moveDir -= rightDir; | |
} | |
if( moveDir.sqrMagnitude > Mathf.Epsilon ) | |
{ | |
moveDir = moveDir.normalized; | |
Turn( moveDir ); | |
Move( moveDir ); | |
} | |
if( Input.GetKeyDown( KeyCode.Space ) ) | |
{ | |
Jump(); | |
} | |
} | |
private void ControlCamera() | |
{ | |
if( Input.GetKey( KeyCode.A ) ) | |
{ | |
m_camera.Turn( m_camraTurnSpeed * Time.deltaTime ); | |
} | |
if( Input.GetKey( KeyCode.D ) ) | |
{ | |
m_camera.Turn( -m_camraTurnSpeed * Time.deltaTime ); | |
} | |
} | |
private void Move( Vector3 i_forward ) | |
{ | |
Vector3 delta = i_forward * m_moveSpeed * Time.deltaTime; | |
Vector3 targetPos = transform.position + delta; | |
m_rigidbody.MovePosition( targetPos ); | |
} | |
private void Turn( Vector3 i_forward ) | |
{ | |
Quaternion toRot = Quaternion.LookRotation( i_forward ); | |
Quaternion fromRot = transform.rotation; | |
float delta = m_turnSpeed * Time.deltaTime; | |
Quaternion targetRot = Quaternion.RotateTowards( fromRot, toRot, delta ); | |
m_rigidbody.MoveRotation( targetRot ); | |
} | |
private void Jump() | |
{ | |
// えっ、このままじゃ空中でもジャンプできちゃうって!? | |
// 仕様だよ! | |
m_rigidbody.velocity = Vector3.zero; | |
Vector3 jumpVec = Vector3.up * m_jumpForce; | |
m_rigidbody.AddForce( jumpVec, ForceMode.VelocityChange ); | |
} | |
} // class ObjectController |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment