Unityでカメラワークの実装
4つのカメラワークの実装
アニメのカメラワークや撮影処理をまとめた記事を読んで、カメラの動作をUnityで再現してみようと思い立ちました。今回、実装したカメラワークは「パン」「フォロー」「じわパン」「ポン寄り」の4つです。用語を深く理解していないので動作の解釈が間違っている可能性があります。利用するときはお気を付けください。あとフォローの挙動が変です。
カメラワークの実装。https://t.co/berx428Qrl pic.twitter.com/VRDIIq4DEq
— miyamo (@DMiyamo3) 2017年7月12日
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraWork : MonoBehaviour { public GameObject target; public enum CameraType { fix, pan, follow, pan_slow, pon } [SerializeField] CameraType camType = CameraType.pan; Vector3 distance; float rotSpeed = 0.1f; float timePass; // Use this for initialization void Start () { if (camType.ToString() == "follow") distance = this.transform.position - target.transform.position; } // Update is called once per frame void Update () { switch (camType.ToString()) { case "fix": break; case "pan": pan(target); break; case "follow": follow(target); break; case "pan_slow": pan_slow(target); break; case "pon": pon(target); break; } } void pan(GameObject target) { this.transform.LookAt(target.transform); } void pan_slow(GameObject target) { float step = rotSpeed * Time.deltaTime; Vector3 vec = target.transform.position - this.transform.position; Quaternion rot = Quaternion.LookRotation(vec); this.transform.rotation = Quaternion.Slerp(transform.rotation, rot, step); } void follow(GameObject target) { this.transform.LookAt(target.transform); this.transform.position = target.transform.position + distance; } void pon(GameObject target) { timePass += Time.deltaTime; if(timePass >= 1) { Camera cam; cam = this.gameObject.transform.GetComponent<Camera>(); float view = cam.fieldOfView - 15; cam.fieldOfView = Mathf.Clamp(value: view, min: 0.1f, max: 90f); if (view < 0) { cam.fieldOfView = 90; } timePass = 0; } this.transform.LookAt(target.transform); } }