Z
Z
zelyy2020-04-23 09:17:07
C++ / C#
zelyy, 2020-04-23 09:17:07

How to make the player jump only from the ground?

when repeatedly swipe up, the player takes off.
The script hangs on the UI panel and therefore fails to process the player's touch with the ground collider so that the player can only jump when he is on the ground.
Is there any code that handles touching a tag with a tag like ( if(tag "Player" == tag "terrain" ) )?

{
    public GameObject player;

    Rigidbody2D rb;

    public float SpeedUp=100;
   


    public void OnBeginDrag(PointerEventData eventData)
    {if ((Mathf.Abs(eventData.delta.y)) > (Mathf.Abs(eventData.delta.x)))//если движение по вертикали больше чем по горизонтали
            if (eventData.delta.y > 0)//если движение вверх по экрану
            {
                rb.AddForce(Vector2.up * SpeedUp, ForceMode2D.Impulse);
               
            }
            else
            {

            }
            
    }
   

    public void OnDrag(PointerEventData eventData)
    {
        
    }

    void Start()
    {
       
        rb = player.GetComponent<Rigidbody2D>();
    }

  
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Griboks, 2020-04-23
@Griboks

Use raycast for touch test and anything (same tag) for ground test.

D
DanielMcRon, 2020-04-23
@DanielMcRon

Either as Griboks said , or via Physics2D. OverlapCirlce, in general, there are a lot of interesting things in Google, including the answer to your question

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question