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. Dismiss Notice

Third person camera script

Discussion in 'Scripting' started by Vexer, Jul 24, 2018.

  1. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Hey guys i've been working on my third person camera script that basically follows the player and looks at the player and i'm kinda stuck right now im trying to clamp/set the camera max and min to rotate up and down but for some reason this is not working any idea how i could do this?
    My second problem is that my camera keeps going through terrain i tried to add a rigid body to it(didn't fix it), added a collision box to it(didn't fix it) so im kinda clueless at this point i would love to hear from you guys have a great day!!
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerFollow : MonoBehaviour {
    7.  
    8.     public Transform PlayerTransform;
    9.  
    10.     private Vector3 _cameraOffset;
    11.  
    12.     public Rigidbody rb;
    13.  
    14.     public bool LookAtPlayer = false;
    15.  
    16.     public bool RotateAroundPlayer = true;
    17.  
    18.     public float RotationsSpeed = 5.0f;
    19.  
    20.     [Range(0.01f, 1.0f)]
    21.     public float SmoothFactor = 0.5f;
    22.  
    23.     // Use this for initialization
    24.     void Start () {
    25.         _cameraOffset = transform.position - PlayerTransform.position;
    26.     }
    27.    
    28.     // FixedUpdate is called after Update methods
    29.     void FixedUpdate () {
    30.  
    31.         if (RotateAroundPlayer)
    32.         {
    33.                 Quaternion camTurnAngleRightandLeft = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * RotationsSpeed, Vector3.up);
    34.                 Quaternion camTurnAngleUpandDown = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * RotationsSpeed, Vector3.right);
    35.  
    36.             _cameraOffset = camTurnAngleUpandDown * camTurnAngleRightandLeft * _cameraOffset;
    37.  
    38.             /*if (Input.GetMouseButton(2))
    39.             {
    40.                 Quaternion camTurnAngleUp = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * RotationsSpeed, Vector3.left);
    41.                 _cameraOffset = camTurnAngleUp * _cameraOffset;
    42.             }*/
    43.         }
    44.  
    45.         Vector3 newPos = PlayerTransform.position + _cameraOffset;
    46.  
    47.         transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);
    48.  
    49.         if (LookAtPlayer || RotateAroundPlayer)
    50.         {
    51.             transform.LookAt(PlayerTransform);
    52.         }
    53.     }
    54. }
    55.  
     
  2. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    I don't see any clamping code.

    As for the camera going through the geometry: what I did was used a CharacterController. Then, instead of simply setting transform.position = some new location, I would add a new variable called idealPosition, then use CharacterController.Move() to try and reach that position. This allows it to slide off of walls. Of course, I do some other checks such as raycasting from where it is to where it wants to be to make sure it has a path, plus checking on whether the player is occluded.
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    With regard to the 'through terrain' -- There is a script in the standard assets for preventing wall clipping. You could try using that.

    I did my best to answer your other question about clamping in another thread. You shouldn't need to duplicate thread questions in a day, for future reference. :)