Fixed camera to follow
This camera has a reference object, it will maintain a fixed position of the reference object, the reference object is moved to follow the change
using UnityEngine; using System.Collections; public class CameraFlow : MonoBehaviour { public Transform target; private Vector3 offset; // Use this for initialization void Start() { offset = target.position - this.transform.position; } // Update is called once per frame void Update() { this.transform.position = target.position - offset; } }
Fixed camera to follow, with the angle of rotation
A camera followed this is an improvement over the first camera to follow, in the original basis of the above, add the control to follow the angle
using UnityEngine; using System.Collections; public class CameriaTrack : MonoBehaviour { private Vector3 offset = new Vector3(0,5,4);//相机相对于玩家的位置 private Transform target; private Vector3 pos; public float speed = 2; // Use this for initialization void Start () { target = GameObject.FindGameObjectWithTag("Player").transform; } // the Update IS Called Once per Frame void the Update () { POS = target.position + offset; the this .transform.position = Vector3.Lerp ( the this .transform.position, POS, Speed * Time.deltaTime); // adjustment camera the distance between the players Quaternion Angel = Quaternion.LookRotation (target.position - the this .transform.position); // Get the rotation angle of the this .transform.rotation = Quaternion.Slerp ( the this .transform.rotation, Angel, Speed * Time .deltaTime); } }
Third-person camera
Such cameras to follow, is a third-person perspective to an object, that is, we have been looking to behind objects, as always display the player's back
the using UnityEngine; the using the System.Collections; // camera has shot lead back public class CameraFlow: MonoBehaviour { public the Transform target; public a float distanceUp = 15f; public a float distanceAway = 1OF; public a float Smooth =. 2F; // location smooth movement value public a float camDepthSmooth = 5F; // the Use for the this Initialization void the Start () { } // the Update IS Called Once per Frame void the Update () { // 鼠标轴控制相机的远近 if ((Input.mouseScrollDelta.y < 0 && Camera.main.fieldOfView >= 3) || Input.mouseScrollDelta.y > 0 && Camera.main.fieldOfView <= 80) { Camera.main.fieldOfView += Input.mouseScrollDelta.y * camDepthSmooth * Time.deltaTime; } } void LateUpdate() { //相机的位置 Vector3 disPos = target.position + Vector3.up * distanceUp - target.forward * distanceAway; transform.position=Vector3.Lerp(transform.position,disPos,Time.deltaTime*Smooth); // camera angle transform.LookAt (target.position); } }
The camera follows the mouse movement and zoom control
The camera and the observation target to maintain a certain distance, can be rotated up and down by a mouse, zoom in and out by operating a mouse wheel
using UnityEngine; using System.Collections; public class CameraFlow : MonoBehaviour { public Transform target; Vector3 offset; // Use this for initialization void Start() { offset = transform.position - target.position; } // Update is called once per frame void Update() { transform.position = target.position + offset; Rotate(); Scale(); } //缩放 private void Scale() { float dis = offset.magnitude; dis += Input.GetAxis("Mouse ScrollWheel") * 5; Debug.Log("dis=" + dis); if (dis < 10 || dis > 40) { return; } offset = offset.normalized * dis; } //左右上下移动 private void Rotate() { if (Input.GetMouseButton(1)) { Vector3 POS = transform.position; Vector3 rot = transform.eulerAngles; // rotation about the origin, may be changed Vector3.zero target.position, is rotated about the observation object transform.RotateAround (Vector3.zero, Vector3. up, Input.GetAxis ( " Mouse X- " ) * 10 ); transform.RotateAround (Vector3.zero, Vector3.left, Input.GetAxis ( " Mouse the Y " ) * 10 ); a float X = transform.eulerAngles.x; a float = Y transform.eulerAngles.y; Debug.Log ( " X = " + X); Debug.Log ( " Y = " + Y); // controlling movement range IF (X < 20 is || X> 45 || Y < 0 || Y> 40 ) { transform.position = POS; transform.eulerAngles = rot; } // update the relative difference offset = transform.position - target.position; } } }