• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by littleappmaker_unity · Feb 26, 2019 at 09:20 AM · raycastraycastingraycasts

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

211 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycasting is running in two different objects with the same Tag when I touch just one of them 1 Answer

How to actually make raycast hit colliders it s starting from? 1 Answer

Raycast Not Detecting Hit 0 Answers

Cursor not staying centered to gameobject 0 Answers

Ignore Raycast not working properly? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges