Search Unity

ThirdPersonCamera Script help

Discussion in 'Scripting' started by Luke3671, Sep 25, 2016.

  1. Luke3671

    Luke3671

    Joined:
    Jul 6, 2014
    Posts:
    56
    Afternoon, I'm still learning to code in unity C# or JS (by using tutorials & things i have leaned on the way) getting more understanding with things ect... (Not sure if this code is all wrong tbf) but basically what i'm trying to do is make it like any other 3rd person camera, atm this script only rotates around the player & everything then goes invented as the player only goes 1 way but camera goes around the player kinda thing is there any tutorials how to do 1 or able to do quick script 4 me?

    The script i have connect to my camera & then linked to my player is below (C#)

    Thank you for your time
    Luke

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ThirdPersonCamera : MonoBehaviour
    5. {
    6.     private const float Y_ANGLE_MIN = 0.0f;
    7.     private const float Y_ANGLE_MAX = 50.0f;
    8.  
    9.     public Transform lookAt;
    10.     public Transform camTransform;
    11.  
    12.     private Camera cam;
    13.  
    14.     private float distance = 10.0f;
    15.     private float currentX = 0.0f;
    16.     private float currentY = 0.0f;
    17.     private float sensivityX = 4.0f;
    18.     private float sensivityY = 1.0f;
    19.  
    20.     private void Start()
    21.     {
    22.         camTransform = transform;
    23.         cam = Camera.main;
    24.     }
    25.  
    26.     private void Update()
    27.     {
    28.         currentX += Input.GetAxis ("Mouse X");  
    29.         currentY += Input.GetAxis ("Mouse Y");
    30.  
    31.         currentY = Mathf.Clamp (currentY, Y_ANGLE_MIN,Y_ANGLE_MAX);
    32.  
    33.     }
    34.  
    35.     private void LateUpdate()
    36.     {
    37.         Vector3 dir = new Vector3 (0, 0, -distance);
    38.         Quaternion rotation = Quaternion.Euler (currentY, currentX, 0);
    39.         camTransform.position = lookAt.position + rotation * dir;
    40.         camTransform.LookAt (lookAt.position);
    41.     }
    42. }
     
  2. Dennis59

    Dennis59

    Joined:
    Jan 8, 2013
    Posts:
    66
    Take a look at the Unity Standard Assets. It has a basic third person camera controller. You can use this as guidance.
     
  3. Benj4_

    Benj4_

    Joined:
    Nov 12, 2015
    Posts:
    38
    I Made A Switch Camera View Script From First Person To Thrid Person But I Don't Know If This Is What You Want...