(まとめ)カメラスクリプト
追跡カメラ
using UnityEngine;
using System.Collections;
public class ChaseCamera : MonoBehaviour {
public GameObject target;
private Vector3 offset;
void Start () {
offset = transform.position - target.transform.position;
}
void Update () {
transform.position = target.transform.position + offset;
}
}
追跡カメラ2(線形補間)
using UnityEngine;
using System.Collections;
public class ChaseCamera : MonoBehaviour {
public GameObject target;
private float smoothing = 2f;
private Vector3 offset;
void Start () {
offset = transform.position - target.transform.position;
}
void Update () {
Vector3 targetCamPos = target.transform.position + offset;
transform.position = Vector3.Lerp(transform.position, targetCamPos, smoothing * Time.deltaTime);
}
}
カメラをターゲットに向かせる
using UnityEngine;
using System.Collections;
public class MainCamera : MonoBehaviour {
public GameObject target;
void LateUpdate(){
// メインカメラをターゲットに向かせる。
Camera.main.transform.LookAt(target.transform);
}
}
Unity Code Memo
他のコースを見る追跡カメラ
using UnityEngine;
using System.Collections;
public class ChaseCamera : MonoBehaviour {
public GameObject target;
private Vector3 offset;
void Start () {
offset = transform.position - target.transform.position;
}
void Update () {
transform.position = target.transform.position + offset;
}
}
追跡カメラ2(線形補間)
using UnityEngine;
using System.Collections;
public class ChaseCamera : MonoBehaviour {
public GameObject target;
private float smoothing = 2f;
private Vector3 offset;
void Start () {
offset = transform.position - target.transform.position;
}
void Update () {
Vector3 targetCamPos = target.transform.position + offset;
transform.position = Vector3.Lerp(transform.position, targetCamPos, smoothing * Time.deltaTime);
}
}
カメラをターゲットに向かせる
using UnityEngine;
using System.Collections;
public class MainCamera : MonoBehaviour {
public GameObject target;
void LateUpdate(){
// メインカメラをターゲットに向かせる。
Camera.main.transform.LookAt(target.transform);
}
}
(まとめ)カメラスクリプト