- Home /
My raycast wont function properly
Hi folks, Relatively new Unity dev here with a quick Question. The goal of this function is to fire a raycast sequentially from 1. The center of the 3D object 2. Both sides of the object on the y axis 3. Both sides of the object on the x axis
If these rays intersect with another object with a collider (e.g any object they could jump or walk on), the function flips a switch(bool) to say if the 3D object is grounded or not. This enables me to code different behavior into the different states. The code works if i remove steps 2 and 3, but it fails to flip the switch when the objects center (transform.position) is hanging over the edge of the object in question. I'd like it to function properly to keep responsiveness and quickness in my jumps.
Here is the code
bool IsGrounded () // Checks if the object is on the ground
{
float _spriteDimensionX = 0.4f;
float _spriteDimensionY = 0.4f;
if(Physics.Raycast (transform.position , - Vector3.up, GroundDistance + 0.51f)==true)
{
Debug.Log ("MID");
return true;
}
else if (Physics.Raycast (transform.position + new Vector3 (_spriteDimensionX , 0.0f, 0.0f) , -Vector3.up, 0.5f)==true)
{
Debug.Log("X");
return true;
}
else if (Physics.Raycast (transform.position+ new Vector3 (-_spriteDimensionX, 0.0f, 0.0f), -Vector3.up,0.5f)==true)
{
Debug.Log ("-X");
return true;
}
else if (Physics.Raycast (transform.position+ new Vector3 (0.0f, _spriteDimensionY, 0.0f), -Vector3.up,0.5f)==true)
{
Debug.Log ("Y");
return true;
}
else if (Physics.Raycast (transform.position+ new Vector3 (0.0f, -_spriteDimensionY, 0.0f), -Vector3.up, 0.5f)==true)
{
Debug.Log ("-Y");
return true;
}
else return false;
}
Your answer
Follow this Question
Related Questions
How to get pixel coordinates when mouse click on Sprite Renderer 0 Answers
Raycast stops working after ~40 seconds when target has Rigidbody 2 Answers
why are child colliders sometimes ignored? 2 Answers
Unable to set collision layer from scrip 0 Answers
Model not translating along its position with Animate Physics option enabled. 0 Answers