Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CameraScript : MonoBehaviour {
- //It's the ship
- public GameObject Ship;
- public float TurnSpeed;
- //How far away the camera should be from the ship
- Vector3 offset;
- void Start()
- {
- //May act funny when not maximaized in editor
- //Hides mouse
- Cursor.visible = false;
- //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.
- offset = Ship.transform.position - transform.position;
- //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.
- }
- void LateUpdate()
- {
- //This code will check if the player exsist within the scene by searching its' name
- if (GameObject.Find("Ship") != null)
- {
- //Capital X
- float horizontal = Input.GetAxis("Mouse X") * TurnSpeed;
- Ship.transform.Rotate(0, horizontal, 0);
- float desiredAngleY = Ship.transform.eulerAngles.y;
- Quaternion rotation = Quaternion.Euler(0, desiredAngleY, 0);
- //Take the distance between the camera and the ship
- transform.position = Ship.transform.position - (rotation * offset);
- //Lookat the Ship
- transform.LookAt(Ship.transform,Ship.transform.up);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment