Search Unity

Question Camera stuttering and showing multiple perspective upon rotation

Discussion in 'Scripting' started by kylebrussell, Dec 15, 2022.

  1. kylebrussell

    kylebrussell

    Joined:
    Oct 5, 2014
    Posts:
    2
    Working from one of the Unity Learn projects where you drive a truck along a road. Wanted to modify the camera controller script to rotate and translate the camera to be in front of the truck while you're pressing the down button. It kind of works, but stutters a ton and seems to show multiple perspectives.

    Could someone help me to rewrite this/learn the proper pattern for implementing a temporary camera state?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FollowPlayer : MonoBehaviour
    6. {
    7.     public GameObject player;
    8.     private Vector3 offset = new Vector3(-2, 5, -7);
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.      
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.  
    20.         if (Input.GetAxis("Vertical") < 0) {
    21.             offset = new Vector3(2, 5, 7);
    22.             transform.Rotate(Vector3.up, 180.0f, Space.World);
    23.         } else offset = new Vector3(-2, 5, -7);
    24.         transform.position = player.transform.position + offset;
    25.     }
    26. }
     
    Last edited: Dec 15, 2022
    Bunny83 likes this.
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    You are rotating your object 180 degrees every frame while you are holding down.
    You will need to figure out a way to restrict this.
    Good luck.
     
    kylebrussell and Bunny83 like this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,992
    I would recommend two changes:
    1. Store the initial rotation of the camera and maybe the offsetted rotation so you can easily switch between them
    2. Use LateUpdate instead of Update. For camera movement this is usually the best place as it ensures the player has been moved already this frame so the camera doesn't lag behind one frame.
    Something like this:

    Code (CSharp):
    1. public class FollowPlayer : MonoBehaviour
    2. {
    3.     public GameObject player;
    4.     private Vector3 offset = new Vector3(-2, 5, -7);
    5.     private Quaternion orientation, orientationRotated;
    6.  
    7.     void Start()
    8.     {
    9.         orientation =  transform.rotation;
    10.         orientationRotated = Quaternion.Euler(0,180f,0) * orientation;
    11.     }
    12.  
    13.     void LateUpdate()
    14.     {
    15.         if (Input.GetAxis("Vertical") < 0)
    16.         {
    17.             offset = new Vector3(2, 5, 7);
    18.             transform.rotation = orientationRotated;
    19.         }
    20.         else
    21.         {
    22.             offset = new Vector3(-2, 5, -7);
    23.             transform.rotation = orientation
    24.         }
    25.         transform.position = player.transform.position + offset;
    26.     }
    27. }
    Like ThermalFusion said, Rotate does a relative rotation from the current orientation of the object. So each frame you would rotate by 180° when calling Rotate. It does not "set" the orientation.

    Another thing you may keep in mind for the future: Instead of declaring a public GameObject variable to reference the player, you can declare a Transform variable and drag the player onto that. That way you don't have to use the transform property on the "player" variable (which essentially just gets the Transform component from the player gameobject). You can directly link to components on other objects that way, not only to Transforms. When you drag an object onto a field in the inspector, Unity tries to find an appropriate type on that gameobject.
     
    ThermalFusion and kylebrussell like this.
  4. kylebrussell

    kylebrussell

    Joined:
    Oct 5, 2014
    Posts:
    2
    Storing the initial and offset rotations was roughly the solution I was imagining, figured I was doing something wrong with If block.

    Thank you so much for helping me learn this new pattern!
     
    Bunny83 likes this.