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

How can you move the camera one way along the Y axis but not the other?

Discussion in '2D' started by AV_Corey, Feb 18, 2016.

  1. AV_Corey

    AV_Corey

    Joined:
    Jan 7, 2016
    Posts:
    122
    I need the camera in my game to follow my player up along the Y axis but not follow the player when they move down the Y axis.

    This is my current camera script:

    Code (CSharp):
    1.  
    2.     [SerializeField]
    3.     private float xMax;
    4.  
    5.     [SerializeField]
    6.     private float yMax;
    7.  
    8.     [SerializeField]
    9.     private float xMin;
    10.  
    11.     [SerializeField]
    12.     private float yMin;
    13.  
    14.     private Transform target;
    15.  
    16.  
    17.     void Start ()
    18.     {
    19.         target = GameObject.Find ("Player").transform;
    20.     }
    21.    
    22.  
    23.     void LateUpdate ()
    24.     {
    25.         transform.position = new Vector3(Mathf.Clamp (target.position.x,xMin,xMax), Mathf.Clamp(target.position.y,yMin,yMax), transform.position.z);                                
    26.     }
    Any help would be appreciated.
     
  2. Mich_9

    Mich_9

    Joined:
    Oct 22, 2014
    Posts:
    118
    You mean the camera is only allowed to go up?
     
  3. AV_Corey

    AV_Corey

    Joined:
    Jan 7, 2016
    Posts:
    122
    Indeed :)
     
  4. toxicFork

    toxicFork

    Joined:
    Sep 15, 2011
    Posts:
    8
    You could set yMin to transform.position.y every frame for example :)

    Or, in the y component, you can do
    Code (CSharp):
    1.  
    2. Mathf.Max(
    3.     Mathf.Clamp(target.position.y,yMin,yMax),
    4.     transform.position.y
    5. )
    6.  
    instead, which will be similar to doing this:

    Code (CSharp):
    1.  
    2. var wantedX = ...;
    3. var wantedY = Mathf.Clamp(target.position.y,yMin,yMax);
    4. var wantedZ = ...;
    5. if(wantedY < transform.position.y) {
    6.     // prevent wantedY from being less than transform.position.y
    7.     wantedY = transform.position.y;
    8. }
    9. transform.position = new Vector3(wantedX, wantedY, wantedZ);
    10.  
     
    AV_Corey likes this.
  5. Mich_9

    Mich_9

    Joined:
    Oct 22, 2014
    Posts:
    118
    Create a variable to store the maximum height reached for the player/camera.
    Code (CSharp):
    1.  
    2. float maxHeight;//maximum height reached
    3.  
    Now in late update save the highest value for y
    Code (CSharp):
    1. float currentHeight = target.position.y;
    2. if (currentHeight > maxHeight){
    3.    maxHeight = currentHeight;
    4. }
    Then use that value for the y axis of the new camera position
    Code (CSharp):
    1. Mathf.Clamp (target.position.y,maxHeight,yMax);
     
    AV_Corey and toxicFork like this.
  6. AV_Corey

    AV_Corey

    Joined:
    Jan 7, 2016
    Posts:
    122
    Thankyou very much <3