using System.Collections; using System.Collections.Generic; using UnityEngine; public class playerController : MonoBehaviour { public float speed = 5f; private float movement = 0f; private Rigidbody2D rigidbody; bool facingRight = true; public float jumpForce = 8f; public Transform groundCheck; public float checkRadious; public LayerMask groundLayer; private bool isTouchingGround; private Animator playAnimation; // Start is called before the first frame update void Start() { rigidbody = GetComponent<Rigidbody2D> (); playAnimation = GetComponent<Animator>(); } // Update is called once per frame void Update() { isTouchingGround = Physics2D.OverlapCircle(groundCheck.position,checkRadious,groundLayer); Debug.Log (isTouchingGround); movement = Input.GetAxis ("Horizontal"); //Horizontal movement rigidbody.velocity = new Vector2 (movement * speed,rigidbody.velocity.y);
//Verical movement
if(Input.GetKeyDown(KeyCode.Space)
&& isTouchingGround){
rigidbody.velocity = new Vector2
(rigidbody.velocity.x,jumpForce);
}//play animation playAnimation.SetFloat ("speed", rigidbody.velocity.x); playAnimation.SetBool ("onGround",isTouchingGround); } }
1 Answer
1Try Physics.CircleCast() to see the info about the collision.
RayCastHit2D hitInfo = Physics.CircleCast(
groundCheck.position, groundCheckRadius, Vector2.down,
Physics.defaultContactOffset, groundLayer);
Debug.Log("Collider=" + hitInfo.collider);
You probably will need to use Vector2.right or -Vector2.forwarddepending on what axes you use in your 2D scene. This vector should point down in the scene. If you have Y axis as vertical, then it will be Vector2.down, and if you vertical is Z axis, then you should use -Vector2.forward I believe.
Also, make sure your groundCheck transform is moving with the rigidBody and not just stands still.