LegitTeddyBears

CameraScript

May 25th, 2017
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CameraScript : MonoBehaviour {
  6.     //It's the ship
  7.     public GameObject Ship;
  8.     public float TurnSpeed;
  9.     //How far away the camera should be from the ship
  10.     Vector3 offset;
  11.  
  12.     void Start()
  13.     {
  14.         //May act funny when not maximaized in editor
  15.         //Hides mouse
  16.         Cursor.visible = false;
  17.         //We are saving the difference in position between whatever object this script is attached to (this case our camera) and the position of the ship.
  18.         offset = Ship.transform.position - transform.position;
  19.         //Ship.transform.position means within the object we designated as ship we will access the transform component and within the transform component we are accessing the position.
  20.     }
  21.  
  22.  
  23.     void LateUpdate()
  24.     {
  25.         //This code will check if the player exsist within the scene by searching its' name
  26.         if (GameObject.Find("Ship") != null)
  27.         {
  28.                         //Capital X
  29.                         float horizontal = Input.GetAxis("Mouse X") * TurnSpeed;
  30.                         Ship.transform.Rotate(0, horizontal, 0);
  31.  
  32.                         float desiredAngleY = Ship.transform.eulerAngles.y;
  33.                         Quaternion rotation = Quaternion.Euler(0, desiredAngleY, 0);
  34.                         //Take the distance between the camera and the ship
  35.                         transform.position = Ship.transform.position - (rotation * offset);
  36.                         //Lookat the Ship
  37.                         transform.LookAt(Ship.transform,Ship.transform.up);
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment