- Home /
isTouchingground always returnig true even i'm noton the ground
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 updatevoid Start(){rigidbody = GetComponent<Rigidbody2D> ();playAnimation = GetComponent<Animator>();}// Update is called once per framevoid Update(){isTouchingGround = Physics2D.OverlapCircle(groundCheck.position,checkRadious,groundLayer);Debug.Log (isTouchingGround);movement = Input.GetAxis ("Horizontal"); //Horizontal movementrigidbody.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 animationplayAnimation.SetFloat ("speed", rigidbody.velocity.x);playAnimation.SetBool ("onGround",isTouchingGround);}}
Answer by Ermiq · Nov 16, 2019 at 07:28 AM
Try 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.
Your answer
Follow this Question
Related Questions
GUI Button to toggle true false when pressed 1 Answer
how false and true player? 1 Answer
Timer & Isgrounded Issue (Getkey not working) 0 Answers
Boolean problem!! 2 Answers
How can I activate a scripts variable from another gameobject? 1 Answer