- Home /
Raycast is not setting false, however is able to be set to true.
I have a raycast that checks to see if the player is grounded. When it hits the ground, the bool "Grounded" is set to true, and when it does not hit the ground, bool is set to false. Problem is that moment it hits the ground it stays true and when it doesn't hit the ground it doesn't change to false unless I change it in the inspector.
private void FixedUpdate(){
if (Grounded)
{
Debug.Log("Grounded");
}
if (Physics.Raycast(PlayerRaycastStartPoint.position,
PlayerRaycastStartPoint.forward, out groundhit, RaycastDistance))
{
if (groundhit.collider.tag == "Ground")
{
Grounded = true;
}
}
else
{
Grounded = false;
}
}
Answer by NathanGG · Jan 03, 2019 at 03:37 PM
Don't use else, use:
if (groundhit.collider.tag !== "Ground") {
Grounded = false;
}
The ! stands for not, so it converts equals to not equals.
I tried doing that before using else, however, it still didn't set to false. I'll try again and hopefully, it works. I probably just typed out the previous one incorrectly.
Now it works so I probably did something wrong last time. Thank you for the help
Answer by Dorscherl · Jan 03, 2019 at 03:29 AM
When your raycast is hitting something it checks the gameobject's tag to determine if its grounded. It doesnt set isGrounded to false if it tag does not equal "Ground".
Answer by Shaolin-Dave · Jan 04, 2019 at 12:01 AM
Try these:
private void FixedUpdate(){
if (Physics.Raycast(PlayerRaycastStartPoint.position, PlayerRaycastStartPoint.forward, out groundhit, RaycastDistance) && groundhit.collider.tag == "Ground")
{
Grounded = true;
} else {
Grounded = false;
}
Debug.Log("Grounded = " Grounded);
Debug.Log("groundhit.collider.tag = " groundhit.collider.tag);
}
or
private void FixedUpdate(){
Grounded = (Physics.Raycast(PlayerRaycastStartPoint.position, PlayerRaycastStartPoint.forward, out groundhit, RaycastDistance) && groundhit.collider.tag == "Ground") ? true : false;
Debug.Log("Grounded = " Grounded);
Debug.Log("groundhit.collider.tag = " groundhit.collider.tag);
}