Search Unity

Stick the camera to the back of the player without smooth effect

Discussion in 'Scripting' started by ReflexiveFox, Jan 20, 2020.

  1. ReflexiveFox

    ReflexiveFox

    Joined:
    Nov 21, 2019
    Posts:
    7
    Hello everyone,
    i'm writing here because i did not found any solution to my question: is it possible to lock the camera position to the back of the player (a tank) without any smooth effect?
    The camera should follow the tank and rotate with the turret (using mouse).
    I am using the "tank controls" as @makeshiftwings said in this thread:
    https://forum.unity.com/threads/wha...d-3rd-person-camera-movement-controls.496821/

    Here is the code of a camera that i found on Internet and can be useful for racing games:
    Code (CSharp):
    1. public class CarCam : MonoBehaviour
    2. {
    3.     Transform rootNode;
    4.     Transform carCam;
    5.     Transform car;
    6.     Rigidbody carPhysics;
    7.  
    8.     [Tooltip("If car speed is below this value, then the camera will default to looking forwards.")]
    9.     public float rotationThreshold = 1f;
    10.    
    11.     [Tooltip("How closely the camera follows the car's position. The lower the value, the more the camera will lag behind.")]
    12.     public float cameraStickiness = 10.0f;
    13.    
    14.     [Tooltip("How closely the camera matches the car's velocity vector. The lower the value, the smoother the camera rotations, but too much results in not being able to see where you're going.")]
    15.     public float cameraRotationSpeed = 5.0f;
    16.  
    17.     void Awake()
    18.     {
    19.         carCam = Camera.main.GetComponent<Transform>();
    20.         rootNode = GetComponent<Transform>();
    21.         car = rootNode.parent.GetComponent<Transform>();
    22.         carPhysics = car.GetComponent<Rigidbody>();
    23.     }
    24.  
    25.     void Start()
    26.     {
    27.         // Detach the camera so that it can move freely on its own.
    28.         rootNode.parent = null;
    29.     }
    30.  
    31.     void FixedUpdate()
    32.     {
    33.         Quaternion look;
    34.  
    35.         // Moves the camera to match the car's position.
    36.         rootNode.position = Vector3.Lerp(rootNode.position, car.position, cameraStickiness * Time.deltaTime);
    37.  
    38.         // If the car isn't moving, default to looking forwards. Prevents camera from freaking out with a zero velocity getting put into a Quaternion.LookRotation
    39.         if (carPhysics.velocity.magnitude < rotationThreshold)
    40.             look = Quaternion.LookRotation(car.forward);
    41.         else
    42.             look = Quaternion.LookRotation(carPhysics.velocity.normalized);
    43.        
    44.         // Rotate the camera towards the velocity vector.
    45.         look = Quaternion.Slerp(rootNode.rotation, look, cameraRotationSpeed * Time.fixedDeltaTime);              
    46.         rootNode.rotation = look;
    47.     }
    48. }
    I am trying to modify it to meet my requirements but it seems it's not possible.
    Any suggestions?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    If you want no smoothing, just set the camera as a child of the tank, no script needed.

    Caveat: You probably don't actually want no smoothing, it feels pretty bad.