- Home /
Collision issues SOLVED
when chaging between two Ground tagged objects my isGrounded boolean gets checked as false ,even to its on a ground object ,here is the code:
public float speed;
public float horizontalSpeed = 2f;
public int jumpHeight = 300;
public bool isGrounded = true;
private Animator animator;
private Rigidbody rigidBody;
void Start()
{
animator = GetComponent<Animator>();
rigidBody = GetComponent<Rigidbody>();
}
void Update()
{
Movememt();
}
void Movememt()
{
animator.SetFloat("WalkSide", Input.GetAxisRaw("Horizontal"));
animator.SetFloat("WalkBlend", Input.GetAxisRaw("Vertical"));
animator.SetFloat("Run", Input.GetAxisRaw("run"));
animator.SetBool("Jump", isGrounded);
if(Input.GetAxisRaw("Vertical") != 0 || Input.GetAxisRaw("Horizontal") != 0)
{
animator.SetBool("Walking", true);
}
else
{
animator.SetBool("Walking", false);
}
if (animator.GetFloat("Run") > 0)
{
speed = 10f;
}
else
{
speed = 5f;
}
//Movement Front Back
float v = Input.GetAxisRaw("Vertical") * speed * Time.deltaTime;
transform.Translate(0, 0, v);
//end movement Front back
//Movement Left Right
float h = Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime;
transform.Translate(h, 0, 0);
//End movement left right
//Movement Mouse
float hMouse = horizontalSpeed * Input.GetAxisRaw("Mouse X");
transform.Rotate(0, hMouse, 0);
//End Movement mouse
//jump
if(Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
rigidBody.AddRelativeForce(Vector3.up * jumpHeight);
}
//end jump
}
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
void OnCollisionExit(Collision collision)
{
if(collision.gameObject.tag == "Ground")
{
isGrounded = false;
}
}
}
Answer by Captain_Pineapple · Jan 22, 2019 at 09:19 AM
Hey there,
your issue is that you can be in collision with more that 1 ground-tagged object at once. Now imagine the following: you collide with a ground object. -> isGrounded will be true now. Then you enter a second ground collision somewhere at the edge between the ground objects. -> isGrounded will be set to true but will already be true. Then after this you leave the first ground collision. -> isGrounded will be set to false even though you are on ground.
As a solution: add some List<Collider> myGroundContacts
where you add and remove all ground tagged collider that you came in contact with. Only if the list is empty on leaving a collider you will truly be off the ground.
A sidenote: When checking for tags i'd recommend to use the builtin function and write collision.gameObject.CompareTag("Ground")
since it is around 10x faster than getting the tag and comparing it. At your current level this will not be an issue but might be when you do hundreds of tag comparisons in one frame.
Hope this helps.
Answer by guilhermevianagui23 · Jan 22, 2019 at 03:53 PM
@Captain_Pineapple Thank your very much! , i would never have this idea , thx
Cool that i could help you. Consider marking the post as answer when your question is resolved.