There is a character (2d), he knows how to jump and run, but he can jump many times due to the fact that there is no test of the sprite on which he stands. How to check the sprite on which it stands before jumping and if it doesn’t jump in the air?

This is how it doesn't work (the newest version of the unit):

using UnityEngine; using System.Collections; public class SimplePlatformController : MonoBehaviour { public float maxSpeed = 10f; public float jumpForce = 700f; bool facingRight = true; bool grounded = false; public Transform groundCheck; public float groundRadius = 0.2f; public LayerMask whatIsGround; public float move; // Use this for initialization // Update is called once per frame void FixedUpdate() { grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround); move = Input.GetAxis("Horizontal"); } void Update() { if (grounded && (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))) { GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpForce)); } GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y); if (move > 0 && !facingRight) Flip(); else if (move < 0 && facingRight) Flip(); if (Input.GetKey(KeyCode.Escape)) { Application.Quit(); } if (Input.GetKey(KeyCode.R)) { Application.LoadLevel(Application.loadedLevel); } } void Flip() { facingRight = !facingRight; Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; } } 
  • If possible, give some manual on writing 2D games on Unity 3D, I will be very grateful!) - Roman Kravets
  • You can check whether the character is in contact with the earth's collider, but then this is a load on physics - IlyaMakarov59rus
  • How to do it? - Roman Kravets
  • @RomanKravets in FixedUpdate you already do it (though not with the collider, but with the layer ... but not the essence) .. The rest of the test is in the jump code. But you are probably afraid to show the code, so you can only guess and guess what the error is in the coffee grounds. - Alexey Shimansky
  • 2
    Your script is quite working. You apparently forgot to do something ... For example, add a groundCheck object to your sprite or hang up a groundCheck collider on this very groundCheck (if it’s not on the player itself and if the checker needs it) or you forgot to add a collider to the ground..or forgot to add a mask for earth or forgot in whatIsGround choose earth, and whatIsGround there and therefore grounded always false ............ or make the variable grounded publicly (or go to Debug mode) and watch when you have it becomes true / false - Alexey Shimansky

2 answers 2

The best solution for such situations would be to abandon the use of collision physics in the direction of mathematics and Raycast . Physics in Unity is a fairly resource-intensive thing + Unity itself by default uses Box2d in the case of 2d physics and works with it as with a black box. That is, if in the calculations of physics itself something will not be accurate enough for you, then it will be rather difficult to correct, and in some cases it is impossible.

In contrast to the functions of searching and checking intersections and collisions in terms of performance, Raycast is a fairly cheap operation. Read more about Raycast here .

As for the jumps in your situation, it is best practice to do a Raycast certain length in the direction of the floor and on the basis of this to conclude whether the character is in the air or on the ground.

A series of fairly detailed tutorials on the topic of creating a 2d platform game, including a full analysis of the physics of motion / jumps / falls here . The only thing is that the tutorial is in English, but the simple version of Unity uses the old one, and there may be some old calls that look different in newer versions of Unity.

    You can do it through layts in animation. Those. if the animation is now falling or jumping, the transition to the animation jump is not possible. But a good tutorila https://habrahabr.ru/post/212309/

    • Only the author does not (go from one animation to another) asks. He asks why the character jumps despite the fact that the character is in the air - Alexey Shimansky
    • If you correctly make Animator in Unity, then you can control the states of the hero. And the jump will not take place in the air. - strevg
    • The structure (transitions between animations) and tools for working with Animator allows you to do a lot, avoiding crutches for coding these things. - strevg