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; } }
FixedUpdateyou 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 ShimanskygroundCheckobject to your sprite or hang up agroundCheckcollider on this verygroundCheck(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 inwhatIsGroundchoose earth, andwhatIsGroundthere and thereforegroundedalwaysfalse............ or make the variablegroundedpublicly (or go to Debug mode) and watch when you have it becomes true / false - Alexey Shimansky