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. Dismiss Notice

Automatic jump

Discussion in '2D' started by JaimieVos, Apr 8, 2016.

  1. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193
    How can i make my animations automatic jump when the ground is higher.
     
  2. EDevJogos

    EDevJogos

    Joined:
    Jul 24, 2014
    Posts:
    73
    Put a trigger on the jump spots and then call the jump animation ?
     
  3. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    You could use raycasting in the walking direction and trigger the jump at a certain distance.
     
  4. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193
    I never worked with raycasting can you give a little explanation?

    I did this but it don't worked very well.
     
    der_r likes this.
  5. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    Sure. What you want to use is this method: http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html

    It takes the origin of where you want to cast the ray and the direction. I don't have access to Unity or Visual Studio right now, so you might need to convert this pseudo-code to actual code to be used in your game. ;)

    Code (CSharp):
    1. var hit = Physics2D.Raycast(transform.position, Vector3.right * direction, distanceToTriggerJump, obstacleLayerMask);
    2.  
    3. // If we walk toward an obstacle ...
    4.  
    5. if(hit.collider != null)
    6. {
    7. // ... and that obstacle is a staircase or whatever ...
    8. var hitAbove = Physics2D.Raycast(transform.position + Vector3.up * heightOfObstacleThatStillTriggersJump, Vector3.right * direction, distanceToTriggerJump, obstacleLayerMask);
    9.  
    10. // .. jump
    11. if(hitAbove.collider == null) Jump();
    12.  
    13. }
     
  6. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193
    Thx!