Last active
August 29, 2015 14:05
-
-
Save EsProgram/9dab7c473421a3785110 to your computer and use it in GitHub Desktop.
Unity:カメラの注目動作(回転移動)/2通りの回転方法を実装
This file contains hidden or 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 UnityEngine; | |
public class CameraAttention : MonoBehaviour | |
{ | |
public GameObject destination; | |
public GameObject lookAtPos; | |
public float moveSpeed; | |
public float rotateSpeed; | |
// Use this for initialization | |
private void Start() | |
{ | |
//アタッチされたオブジェクトがカメラでなければスクリプトを破棄 | |
if(GetComponent<Camera>() == null) | |
Destroy(this); | |
} | |
// Update is called once per frame | |
private void Update() | |
{ | |
//何かキーを押してる間回転移動する | |
if(Input.anyKey) | |
if(Vector3.Distance(this.transform.position, destination.transform.position) > 0.1f) | |
{ | |
//2通りの回転方法 | |
//transform.LookAt(Vector3.Lerp(this.transform.position, lookAtPos.transform.position, Mathf.Min(Time.time * rotateSpeed, 1))); | |
transform.LookAt(LerpAngle(this.transform.rotation, lookAtPos.transform.rotation, Mathf.Min(Time.time * rotateSpeed, 1))); | |
//移動 | |
transform.Translate((destination.transform.position - this.transform.position) * moveSpeed * Time.deltaTime); | |
} | |
} | |
/// <summary> | |
/// クォータニオンを保管して返す | |
/// </summary> | |
/// <param name="a">origin</param> | |
/// <param name="b">destination</param> | |
/// <param name="t">[0 ~ 1]</param> | |
/// <returns></returns> | |
private Vector3 LerpAngle(Quaternion a, Quaternion b, float t) | |
{ | |
var x = Mathf.LerpAngle(a.x, b.x, t); | |
var y = Mathf.LerpAngle(a.y, b.y, t); | |
var z = Mathf.LerpAngle(a.z, b.z, t); | |
return new Vector3(x, y, z); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment