Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Camera3 : MonoBehaviour {
- // Se Ti interessano i commenti leggili, senno cancellali o ignorali a tua discrezione
- public Transform target; // the transform of the target which the camera should look
- public float turnSpeed = 4f; // how fast the camera moves from inputs
- public float height = 3f; // basically camera's highness respect it's target
- public float distance = 3f; // the back position of the camera respect the target
- public bool is3rdPerson = true; // check state if the player is using a 3rd person setting
- private Vector3 offset; // the initial offset of the camera respect it's target
- private float y = 0f; // the y input from the mouse is stored here
- private float z = 0f; // the z input from the mouse is stored here
- void Start () {
- offset = new Vector3(0, height, -distance); // - distance is for placing the initial camera behind the target instead in front of it
- }
- void Update () {
- // 3rd person cam setting handler
- if (is3rdPerson)
- {
- y = Input.GetAxis("Mouse Y") * turnSpeed;
- z = Input.GetAxis("Mouse X") * turnSpeed;
- // Quaternion.AngleAxis just creates rotation of the angles of the first parameter on the axis indicated by the second parameter
- // Vector3.Up is just a shorthand for new Vector3(0,1,0)
- offset = Quaternion.AngleAxis(z, Vector3.up) * offset;
- offset = Quaternion.AngleAxis(y, Vector3.right) * offset;
- // updates the camera position according to the rotation obtained by mouse inputs
- transform.position= target.position + offset;
- // while moving the camera will look at the transform of the target(the player)
- transform.LookAt(target.position);
- }
- }
- }
Add Comment
Please, Sign In to add comment