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

Might be a simple Question.. Question about locking and unlocking Vector3 coordinates

Discussion in 'Scripting' started by Maxske, Jul 23, 2015.

  1. Maxske

    Maxske

    Joined:
    Feb 20, 2015
    Posts:
    37
    transform.position = new Vector3(transform.position.x,45.6f,transform.position.z);

    if i write this code, then want to unlock the y coordinate in an If statement, how do i go about doing it ?
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    You mean like:
    Code (csharp):
    1. if(someBoolean)
    2.     transform.position = new Vector3(transform.position.x, 45.6f, transform.position.z);
    ?

    I think I may be misunderstanding your question. If you only want the y value to be locked to a specific number in the case that a boolean value is on or off or w/e, then the above code should work fine. If you mean something else, then I'm not following.
     
  3. Maxske

    Maxske

    Joined:
    Feb 20, 2015
    Posts:
    37
    i understand what you are saying but i have this problem. When i am in water, and go to the surface which is y = 45.6f, i want to lock that position since if i move a certain way, i walk into the air.
    So i want to lock that position until i press a button.
    if(Input.GetKey("q"))
    {
    // I AM ABLE TO MOVE THE Y AGAIN HERE
    }
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    You mean like this?
    Code (csharp):
    1. private bool locked = false;
    2.  
    3. public void SomeFunction()
    4. {
    5.     if(Input.GetKey("q"))
    6.         locked = false;
    7.  
    8.     if(!locked && transform.position.y > 45.6f)
    9.         locked = true;
    10.  
    11.     if(locked)
    12.         transform.position = new Vector3(transform.position.x, 45.6f, transform.position.z);
    13. }
    You should probably also use a second boolean value like "in water" to determine if you're maneuvering yourself out of the water onto land, or else re-diving, when the "lock" breaks. Something like that...
     
  5. Maxske

    Maxske

    Joined:
    Feb 20, 2015
    Posts:
    37
    Im having a stupid problem which I cant solve. When i use that correct code, and i hit the 45.6 > y , instead of locking the y, it teleports me into the air and when i land at 45.6, i teleport again into the air, idk why
     
  6. Maxske

    Maxske

    Joined:
    Feb 20, 2015
    Posts:
    37
    Code (CSharp):
    1. else
    2.         {
    3.             inWater = true;
    4.  
    5.             //rigidbody.isKinematic = false;
    6.             chMotor.movement.gravity = 2;
    7.             chMotor.movement.maxFallSpeed = 4;
    8.             chMotor.movement.maxForwardSpeed = 4;
    9.             chMotor.movement.maxSidewaysSpeed = 4;
    10.  
    11.             if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
    12.                 {
    13.                 chMotor.movement.maxForwardSpeed = 7;
    14.                 chMotor.movement.maxSidewaysSpeed = 7;
    15.                 staminaInfo.staminaBar.fillAmount -= 0.001f;
    16.                 }
    17.                 else
    18.                 {
    19.                     chMotor.movement.maxForwardSpeed = 4;
    20.                     chMotor.movement.maxSidewaysSpeed = 4;
    21.                 }
    22.  
    23.             if(Input.GetKey("q"))
    24.             {
    25.                 locked = false;
    26.                 rigidbody.AddForce(Vector3.down * 5, ForceMode.VelocityChange);
    27.  
    28.                 if(Physics.Raycast(transform.position, down, out hit,3))
    29.                    {
    30.                         if(hit.collider.gameObject.tag == "ground")
    31.                         {
    32.                         rigidbody.isKinematic = true;
    33.                         }
    34.                        
    35.                     }
    36.             }
    37.             else
    38.             {
    39.                 rigidbody.isKinematic = false;  
    40.             }
    41.  
    42.    
    43.  
    44.         }
    45.  
    46.         if(inWater == true)
    47.         {
    48.  
    49.             chMotor.jumping.enabled = false;
    50.             if(transform.position.y < 45.6)
    51.             {
    52.                 if(Input.GetKey("space"))
    53.                 {
    54.                 rigidbody.AddForce(Vector3.up * 5, ForceMode.VelocityChange);
    55.                 }
    56.             }
    57.  
    58.             if(!locked && transform.position.y > 45.6f)
    59.             {
    60.                 locked = true;
    61.             }
    62.  
    63.             if(locked)
    64.             {
    65.                 transform.position = new Vector3(transform.position.x, 45.6f, transform.position.z);
    66.             }
    67.                
    68.  
    69.         }