AL4ST4I2

CameraController

Oct 29th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Camera3 : MonoBehaviour {
  5.  
  6.     // Se Ti interessano i commenti leggili, senno cancellali o ignorali a tua discrezione
  7.  
  8.     public Transform target;            // the transform of the target which the camera should look
  9.  
  10.     public float turnSpeed = 4f;        // how fast the camera moves from inputs
  11.     public float height = 3f;           // basically camera's highness respect it's target
  12.     public float distance = 3f;         // the back position of the camera respect the target
  13.     public bool is3rdPerson = true;     // check state if the player is using a 3rd person setting
  14.  
  15.     private Vector3 offset;     // the initial offset of the camera respect it's target
  16.     private float y = 0f;       // the y input from the mouse is stored here
  17.     private float z = 0f;       // the z input from the mouse is stored here
  18.  
  19.     void Start () {
  20.         offset = new Vector3(0, height, -distance); // - distance is for placing the initial camera behind the target instead in front of it
  21.     }
  22.    
  23.    
  24.     void Update () {
  25.         // 3rd person cam setting handler
  26.         if (is3rdPerson)
  27.         {
  28.             y = Input.GetAxis("Mouse Y") * turnSpeed;
  29.             z = Input.GetAxis("Mouse X") * turnSpeed;
  30.  
  31.             // Quaternion.AngleAxis just creates rotation of the angles of the first parameter on the axis indicated by the second parameter
  32.             // Vector3.Up is just a shorthand for new Vector3(0,1,0)
  33.             offset = Quaternion.AngleAxis(z, Vector3.up) * offset;
  34.             offset = Quaternion.AngleAxis(y, Vector3.right) * offset;
  35.  
  36.             // updates the camera position according to the rotation obtained by mouse inputs
  37.             transform.position= target.position + offset;
  38.             // while moving the camera will look at the transform of the target(the player)
  39.             transform.LookAt(target.position);
  40.         }
  41.        
  42.     }
  43. }
Add Comment
Please, Sign In to add comment