Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Free third person camera script.

Discussion in 'Scripting' started by Haravin, Oct 17, 2017.

  1. Haravin

    Haravin

    Joined:
    Aug 21, 2017
    Posts:
    6
    I made a very basic third person camera, which is public domain.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. //This is a camera script made by Haravin (Daniel Valcour).
    4. //This script is public domain, but credit is appreciated!
    5.  
    6. [RequireComponent(typeof(Camera))]
    7. public class CameraSpectator : MonoBehaviour {
    8.  
    9.     public float moveSpeed;
    10.     public float shiftAdditionalSpeed;
    11.     public float mouseSensitivity;
    12.     public bool invertMouse;
    13.     public bool autoLockCursor;
    14.  
    15.     private Camera cam;
    16.  
    17.     void Awake () {
    18.         cam = this.gameObject.GetComponent<Camera>();
    19.         this.gameObject.name = "SpectatorCamera";
    20.         Cursor.lockState = (autoLockCursor)?CursorLockMode.Locked:CursorLockMode.None;
    21.     }
    22.    
    23.     void Update () {
    24.         float speed = (moveSpeed + (Input.GetAxis("Fire3") * shiftAdditionalSpeed));
    25.         this.gameObject.transform.Translate(Vector3.forward * speed * Input.GetAxis("Vertical"));
    26.         this.gameObject.transform.Translate(Vector3.right * speed * Input.GetAxis("Horizontal"));
    27.         this.gameObject.transform.Translate(Vector3.up * speed * (Input.GetAxis("Jump") + (Input.GetAxis("Fire1") * -1)));
    28.         this.gameObject.transform.Rotate(Input.GetAxis("Mouse Y") * mouseSensitivity * ((invertMouse) ? 1 : -1), Input.GetAxis("Mouse X") * mouseSensitivity * ((invertMouse) ? -1 : 1), 0);
    29.         this.gameObject.transform.localEulerAngles = new Vector3(this.gameObject.transform.localEulerAngles.x, this.gameObject.transform.localEulerAngles.y, 0);
    30.  
    31.         if (Cursor.lockState == CursorLockMode.None && Input.GetMouseButtonDown(0))
    32.         {
    33.             Cursor.lockState = CursorLockMode.Locked;
    34.         }
    35.         else if (Cursor.lockState == CursorLockMode.Locked && Input.GetKeyDown(KeyCode.Escape))
    36.         {
    37.             Cursor.lockState = CursorLockMode.None;
    38.         }
    39.     }
    40. }
    41.  
     
    Dustin27 and TRNO like this.
  2. patchtheace

    patchtheace

    Joined:
    Sep 30, 2019
    Posts:
    1
    Borrowing this for a project to learn how the heck movement and cameras work in unity thanks for the help full credit if i ever do anything with the code.
     
    Dustin27 likes this.
  3. MarchantMitton

    MarchantMitton

    Joined:
    Apr 9, 2020
    Posts:
    1
    Hello is there an easy way to make this 3rd person
     
  4. ShamusO

    ShamusO

    Joined:
    Jan 12, 2016
    Posts:
    6
    This should be third person according to the title.
     
  5. CardinalHeathcliff

    CardinalHeathcliff

    Joined:
    Jan 16, 2022
    Posts:
    2
    well yea, attach the main camera to your player gameobject then in the player's script in the update function copy this:
    Code (CSharp):
    1. float mouseX = Input.GetAxis("Mouse X");
    2.      
    3.         x += 1 * mouseX;
    4.      
    5.         Quaternion angle = new Quaternion();
    6.         angle.eulerAngles = new Vector3(0, transform.rotation.y + x, 0);
    7.         transform.rotation = angle;
    8.  
    9.      
    10.         Camera.main.transform.LookAt(transform);
    11.         Camera.main.transform.Translate(Vector3.right * mouseX * Time.deltaTime * 2.8f);//the 2.8f is the sensibility
    12.  
     
  6. dgaban

    dgaban

    Joined:
    Jan 17, 2018
    Posts:
    6
    this script works great but i have to ask, how do you hide walls/obstructions that get in front of the camera? any good solution for that?