Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 oedandthewhale · Jul 03, 2017 at 02:40 PM · c#rotationraycastvector3enemy ai

Help with raycast for for wall detection

I am working on a script which makes my enemy object patrol around a randomly generated map. What my script does is it has the enemy move forward until it reaches a certain distance from any wall right in front of it where it sets the speed variable to 0 so that forward motion stops. The next step it goes through (and this is where the issue is) is it does a ray cast 45 degrees to the left to see if there is a wall then looks to the right to see if there is a wall. if there is no wall on the left it runs left and checks the front again. if it detects the wall on the left it looks to the right and goes through a similar process. once there is no longer a detected wall in front the script changes the speed variable back to the enemy's starting speed so that it can move again. in the even that there is a wall to the left and right I set it to randomly choose left or right with a slight bias to left.

The main issue I am having is that when the object detects the wall in front it just stutters back and forth and doesn't rotate more than a fraction of a degree in either direction, which I can see in the transform component of the enemy object. I attempted to troubleshoot this my self and found that the program is never getting to the part of the script where it rotates so it might be something with that but I have been unable to determine for sure. This is not my full script, only the function I made to do the searching, the rest is just a FixedUpdate which calls the function and then does a vector3 transform forward based on the speed variable changed during the search function Any help, comments, criticism is appreciated

 public void search(){
         RaycastHit hit;
         Ray ray = new Ray(transform.position, Vector3.forward);
         if (Physics.Raycast (ray, out hit, wallDistance)) {
             transform.Translate (Vector3.forward * Time.deltaTime * speed, Space.Self);
             speed = killSpeed;
             test1 = 3;
             Ray rayLeft = new Ray (transform.position, vecLeft);
             Ray rayRight = new Ray (transform.position, vecRight);
             // the if statments in this section check the left, then the right to see if it is clear. if the way is clear for left it will immediatly go left, if not it will check
             // the right then go right if it is clear. if both directions are obstructed it will randomly choose left or right with a bias to right and check again. if the path is
             // clear it will move forward
             if (Physics.Raycast (rayLeft, out hit, wallDistance)) {
                 if (Physics.Raycast (rayRight, out hit, wallDistance)) {
                     test1 = 5;
                     float path = Random.Range(0.0f,1f);
                     if (path >= 0.65f) {
                         transform.rotation = Quaternion.Slerp (transform.rotation, left, Time.deltaTime);
                     } else {
                         transform.rotation = Quaternion.Slerp (transform.rotation, right, Time.deltaTime);
                     }
                 } else {
                     transform.rotation = Quaternion.Slerp (transform.rotation, right, Time.deltaTime);
                 }
             } else {
                 transform.rotation = Quaternion.Slerp (transform.rotation, left, Time.deltaTime);
             }
         } else {
             test2 = 5;
             speed = startSpeed;
         }
     }
Comment
Add comment · Show 1
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
avatar image oedandthewhale · Jul 03, 2017 at 02:47 PM 0
Share

I almost forgot the definitions for left and right Quaternion left = Quaternion.Euler(-0.1f,0,1); Quaternion right = Quaternion.Euler(0.1f,0,1); Vector3 movement = new Vector3 (0.0f, 0.0f, 1f);

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Major · Jul 03, 2017 at 03:41 PM

I think your problem is with the logic that starts at line 13. It reads:

 if we hit a wall left
      if we hit a wall right
           pick random direction

When it should read:

 if we didnt hit a wall left
      if we didnt hit a wall right
           pick random direction

Comment
Add comment · Show 6 · Share
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
avatar image oedandthewhale · Jul 03, 2017 at 05:53 PM 0
Share

I am not entirely sure how to use the negative form of my original statement, any suggestions?

avatar image Major oedandthewhale · Jul 03, 2017 at 06:52 PM 0
Share

You can either do a couple things:

 Physics.Raycast () == false
 
 or 
 
 !Physics.Raycast ()
avatar image oedandthewhale · Jul 03, 2017 at 05:55 PM 0
Share

also the statement you mentioned is for the case if there is a wall in front of, and on both the left and right

avatar image Major oedandthewhale · Jul 03, 2017 at 06:58 PM 0
Share

I just outlined the logic for the entire statement (not including the wall in front check since that's a given). I outlined it, and I think that may be the problem because you are picking a random direction to go in when there are walls to either side. From here you end up walking into another wall, and you start the process over. To me, and without better knowledge about the code, this doesn't seem right. I think what the intended effect is for the code to pick a random direction if there is a wall in front, but not walls to either side. That's why I think you need to be checking if there isn't a wall, rather than if there is.

avatar image oedandthewhale · Jul 04, 2017 at 05:15 AM 0
Share

I made the edits to the logic but I'm still getting the same issue, any suggestions?

 public void search(){
         RaycastHit hit;
         Ray ray = new Ray(transform.position, Vector3.forward);
         if (Physics.Raycast (ray, out hit, wallDistance)) {
             transform.Translate (Vector3.forward * Time.deltaTime * speed, Space.Self);
             speed = killSpeed;
             test1 = 3;
             Ray rayLeft = new Ray (transform.position, vecLeft);
             Ray rayRight = new Ray (transform.position, vecRight);
             
             if (!Physics.Raycast (rayLeft, out hit, wallDistance)) {
                 if (!Physics.Raycast (rayRight, out hit, wallDistance)) {
                     test1 = 5;
                     float path = Random.Range (0.0f, 1f);
                     if (path >= 0.65f) {
                         transform.rotation = Quaternion.Slerp (transform.rotation, left, Time.deltaTime);
                     } else {
                         transform.rotation = Quaternion.Slerp (transform.rotation, right, Time.deltaTime);
                     }
                 } else {
                     transform.rotation = Quaternion.Slerp (transform.rotation, left, Time.deltaTime);
                 }
             } else if (!Physics.Raycast (rayRight, out hit, wallDistance)) {
                 transform.rotation = Quaternion.Slerp (transform.rotation, right, Time.deltaTime);
             } else {
                 float path = Random.Range(0.0f,1f);
                 if (path >= 0.65f) {
                     transform.rotation = Quaternion.Slerp (transform.rotation, left, Time.deltaTime);
                 } else {
                     transform.rotation = Quaternion.Slerp (transform.rotation, right, Time.deltaTime);
                 }
             }
         } else {
             test2 = 5;
             speed = startSpeed;
         }
     }
avatar image Major oedandthewhale · Jul 04, 2017 at 05:09 PM 0
Share

Okay, well another think I'm noticing is the left and right quaternions. They have a value of 0.1 and -0.1 on the x component, and I am pretty sure that needs to be in degrees. Additionally, it does not make sense for the rotation to be on the x component. If it's in 3D space, then I think it should be on the y component. If it is a 2D game, it should be the z component.

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.

Follow this Question

Answers Answers and Comments

396 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 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

Rotated object raycasting in wrong directions!!? 3 Answers

Grabbing the Relative eulerAngles.y of a Rotation 1 Answer

Why isnt my raycast hitting the floor? 0 Answers

Vector3 and transform.rotation? 2 Answers

Track an object and fire? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges