Search Unity

Make a rigidbody jump in correct directions

Discussion in 'Scripting' started by Nicholas1010, Jun 1, 2016.

  1. Nicholas1010

    Nicholas1010

    Joined:
    Aug 2, 2014
    Posts:
    20
    Hello. I made a simple player control script.

    And this is what I use to jump:

    Code (csharp):
    1.  
    2. if (Input.GetButton("Jump"))
    3. {
    4.    GetComponent<Rigidbody>().velocity += new Vector3(0, jumpSize, 0) + transform.forward * 2f;
    5.  
    6. }
    7.  
    I want to make it so that whenever you walk, you would jump in that direction that you are walking, but current one I got only works for whatever you are facing, so if I go backwards and jump, it jumps forward.
     
  2. Nicholas1010

    Nicholas1010

    Joined:
    Aug 2, 2014
    Posts:
    20
    Sorry I accidently posted this thread early.

    So as I said, I want it so that whenever you go right, but you are facing front, it will jump right, like a regular jump that is used in most games.

    jumpSize is a float.

    Any ideas on how to do this?
     
  3. MrUnecht

    MrUnecht

    Joined:
    Jan 16, 2016
    Posts:
    30
    Code (csharp):
    1.  
    2. if (Input.GetButton("Jump"))
    3. {
    4. // add condition here where you facing
    5. If(facing="right"){
    6.    GetComponent<Rigidbody>().velocity += new Vector3(0, jumpSize, 0) + transform.forward * 2f;
    7. }
    8. If (facing="left"){
    9.    GetComponent<Rigidbody>().velocity += new Vector3(0, jumpSize, 0) + transform.backward * 2f;
    10. }
    11. }
    12.  
    You could do this check in the Update Function, and set the string facing to left or right, whenever you tap the jump button then it checks your facing direktion and moves you left or right