Multiple raycasts and only two are working
I am working on an ai and I'd like to give it the distance between the player (a car) and (almost) everything else in the game (walls, middle of the street, other cars). I'm doing it with Physics.Raycast. The problem is that only DistanceToRightSideOfMOTS (MOTS: middle of the street) and DistanceToBackWall are working. This is my code:`private void FixedUpdate() { RequestDecision(); RequestAction();
// Get the distances to the walls
// Get distance to left wall
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out hit, 25, 1 << 11))
{
DistanceToLeftWall = hit.distance / 25;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.left) * hit.distance, Color.red);
}
else
{
DistanceToLeftWall = 1;
}
// Get distance to right wall
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out hit, 25, 1 << 12))
{
DistanceToRightWall = hit.distance / 25;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.right) * hit.distance, Color.red);
}
else
{
DistanceToRightWall = 1;
}
// Get distance to front wall (it sees 2x farther because it's hard to turn on the corner)
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, 50, (1 << 11) | (1 << 12)))
{
DistanceToFrontWall = hit.distance / 50;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.red);
}
else
{
DistanceToFrontWall = 1;
}
// Get distance to back wall
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out hit, 50, (1 << 11) | (1 << 12)))
{
DistanceToBackWall = hit.distance / 50;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.back) * hit.distance, Color.red);
}
else
{
DistanceToBackWall = 1;
}
//////////////////
// Get the distance to other cars
/////////////////
// Get distance to left car
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out hit, 25, 1 << 13))
{
DistanceToLeftCar = hit.distance / 25;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.left) * hit.distance, Color.green);
}
else
{
DistanceToLeftCar = 1;
}
// Get distance to right car
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out hit, 25, 1 << 13))
{
DistanceToRightCar = hit.distance / 25;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.right) * hit.distance, Color.green);
}
else
{
DistanceToRightCar = 1;
}
// Get distance to front car
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, 50, 1 << 13))
{
DistanceToFrontCar = hit.distance / 50; // We divide the distance to normalize the input
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.green);
}
else
{
DistanceToFrontCar = 1;
}
// Get distance to back car
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out hit, 50, 1 << 13))
{
DistanceToBackCar = hit.distance / 50;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.back) * hit.distance, Color.green);
}
else
{
DistanceToBackCar = 1;
}
//Get distances to the middle of the street
//Get distance from the left side
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out hit, 10, 1 << 14))
{
DistanceToLeftSideOfMOTS = hit.distance / 10;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.left) * hit.distance, Color.blue);
}
else
{
DistanceToLeftSideOfMOTS = 1;
}
//Get distance from the right side
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out hit, 10, 1 << 14))
{
DistanceToRightSideOfMOTS = hit.distance / 10;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.right) * hit.distance, Color.blue);
}
else
{
DistanceToRightSideOfMOTS = 1;
}
}` Other distances always return 1 even if it shouldn't. Why isn't working and how can I solve it? Thank you for your answer.
Edit: I don't know why but when I bring the car super close to the right wall, DistanceToRightWall starts working and DistanceToBackCar only works when I'm at least 15m away from a car. Is there any alternative?
Your answer
Follow this Question
Related Questions
Ignore Raycast not working properly? 0 Answers
Raycasting is running in two different objects with the same Tag when I touch just one of them 1 Answer
Cursor not staying centered to gameobject 0 Answers
Raycast Not Detecting Hit 0 Answers
How to actually make raycast hit colliders it s starting from? 1 Answer