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

How do I make it so that I can move up and down along the global y axis?

Discussion in 'Scripting' started by Lilgrinde, Sep 3, 2022.

  1. Lilgrinde

    Lilgrinde

    Joined:
    May 9, 2021
    Posts:
    7
    this is my script for moving the player, im making a Subnautica demake so this is for swimming. I can swim towards wherever the camera is facing, but i also wanna be able to swim up and down along the global y axis and I can't figure out a way to do it, any help would be greatly appreciated.

    this is a code snippet making the player move from my movement script, i apologize if it's bad, I'm not very experienced.

    Code (CSharp):
    1.  
    2. //vertical is the w-s axis
    3. //horizontal is the a-d axis
    4. //Cam is the camera
    5. //mouseY is mouseInput along the Y axis
    6. //mouseX is mouseInput along the X axis
    7.  
    8. void Update()
    9. {
    10.       if (Input.GetKeyDown(KeyCode.Space))
    11.             {
    12.                 goingUp = true;
    13.             }
    14.             if (Input.GetKeyUp(KeyCode.Space))
    15.             {
    16.                 goingUp = false;
    17.             }
    18.  
    19.             if (Input.GetKeyDown(KeyCode.C))
    20.             {
    21.                 goingDown = true;
    22.             }
    23.             if (Input.GetKeyUp(KeyCode.C))
    24.             {
    25.                 goingDown = false;
    26.             }
    27.  
    28.             Vector3 move = Cam.right * horizontal + Cam.forward * vertical + Cam.forward * vertical;
    29.  
    30.             if (goingUp && !goingDown)
    31.             {
    32.                //make the player go up along the global y axis
    33.             }
    34.  
    35.             if (goingDown && !goingUp)
    36.             {
    37.                 //make the player go down along the global y axis
    38.             }
    39.             if (!goingDown && !goingUp)
    40.             {
    41.                 move = Cam.right * horizontal + Cam.forward * vertical + Cam.forward * vertical;
    42.             }      
    43.  
    44.             Cam.Rotate(-mouseY, 0, 0);
    45.            
    46.             transform.Rotate(0, mouseX, 0);          
    47.  
    48.             rb.velocity = new Vector3(move.x * SwimSpeed, move.y * SwimSpeed, move.z * sideSwimSpeed);
    49. }
    50.  
     
  2. Lilgrinde

    Lilgrinde

    Joined:
    May 9, 2021
    Posts:
    7
    I came up with the solution myself, feeling kinda dumb now.

    Code (CSharp):
    1. if (goingUp && !goingDown)
    2.             {
    3.                 rb.AddForce(0, 100, 0);              
    4.             }
    5.  
    6.             if (goingDown && !goingUp)
    7.             {
    8.                 rb.AddForce(0, -100, 0);              
    9.             }