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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How do I freeze the camera from following an object at a certain point?

Discussion in 'Scripting' started by TheRealBingies24, Nov 28, 2020.

  1. TheRealBingies24

    TheRealBingies24

    Joined:
    Nov 26, 2020
    Posts:
    3
    I want to stop the camera from from following an object on the Z axis at a certain spot.

    Code (CSharp):
    1. {
    2.     public float distance;
    3.     public float height;
    4.     public GameObject objectToFollow;
    5.  
    6.  
    7.     // Update is called once per frame
    8.     void LateUpdate()
    9.     {
    10.         if(objectToFollow == null)
    11.             return;
    12.        
    13.         Vector3 destination = objectToFollow.transform.position;
    14.         destination.x = 0f;
    15.         destination.y += height;
    16.         destination.z += distance;
    17.  
    18.         transform.position = destination;
    19.  
    20.        
    21.     }
    22. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
  3. TheRealBingies24

    TheRealBingies24

    Joined:
    Nov 26, 2020
    Posts:
    3
    Maybe an if statement will do the trick, but how do I set the position of the Z axis that it stops on?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    I think you have not fully defined (at least to me) what you're trying to do.

    Do you want the tracking motion to stop completely? That's what I suggest above.

    Do you want the motion to ONLY stop on the Z axis at some point but continue tracking the X and Y axes? If so then when you reach that point, snapshot the Z value and intentionally override it in the controller.

    Remember there is also Cinemachine available free from Unity (google, or get it from the package manager), which can help you define Camera volumes so the camera never goes outside of a particular area, for instance.
     
  5. TheRealBingies24

    TheRealBingies24

    Joined:
    Nov 26, 2020
    Posts:
    3
    How do I use Cinemachine in this case?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Definitely start with some tutorials that seem to be similar to your intended use. As I said above, I suspect I don't understand your needs because of your second post contradicting my assumptions.