Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by coder127 · Mar 13, 2015 at 01:30 PM · collisionraycast3dai

Problem with using Ray Casting to avoid obstacles and walls..

Hi

I'm working on an AI demo showing steering behaviours and I've managed to get the behaviours working. The only issue I'm having is with collision and wall avoidance. I'm trying to use ray casting to detect if there is an obstacle in front of the ai (chicken) and if there is, move away from it but to no avail.

Here's the ray casting code:

 void avoidObstacles(GameObject gob)
     {
         Vector3 ahead = gob.transform.TransformDirection (Vector3.forward);
         RaycastHit hit;
         ahead = ahead.normalized;
 
         if(Physics.Raycast(gob.transform.position, ahead, out hit))
         {
             Debug.DrawRay(gob.transform.position, ahead, Color.red);
             Evade(gob, hit.transform.gameObject);
         }
 
     }

which I'm running in the update function:

void Update () {

     //for every chicken follower, change their behaviour
     foreach(GameObject gob in chickens)
     {
         avoidObstacles(gob);
     }

...

and here is the Evade function code I'm using to move away from the obstacle ahead:

 void Evade(GameObject obj, GameObject obstacle)
     {
         //find direction of target
         Vector3 direction = obj.transform.position - obstacle.transform.position;
         direction.y = 0.0f;
 
         //calculate distance
         float distance = direction.magnitude;
 
         //if the target comes within proximity
         if(distance < 2.0f)
         {
             //rotate away from it
             obj.transform.rotation = Quaternion.Slerp(obj.transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
 
             //move away from it
             moveVector = direction.normalized * moveSpeed * Time.deltaTime;
             obj.transform.position += moveVector;
         }
     }

so yea, unfortunately this isn't working and my chickens disregard there being any obstacles and walk straight into them. Any help in figuring out this problem would be much appreciated.

Thanks in advance!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JinJin · Mar 13, 2015 at 08:54 PM

use

 Vector3 ahead = gob.transform.forward;
 float rayLength = 100.0f;
 if(Physics.Raycast(gob.transform.position, ahead, out hit, rayLength))

instead of

 Vector3 ahead = gob.transform.TransformDirection (Vector3.forward);
 if(Physics.Raycast(gob.transform.position, ahead, out hit))

and draw the ray of the direction you are trying to set. also, try setting the roation without Slerp, just to see if it works

Comment
Add comment · Show 4 · 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 coder127 · Mar 14, 2015 at 08:54 PM 0
Share

hi jinjin thank you for your reply!.

I tried changing the Vector3 ahead like you suggested but makes no difference.

I tried to set rotation without slerp, so thought of using Quaternion.FromToRotation but this still doesn't work. :(

avatar image JinJin · Mar 15, 2015 at 01:22 PM 0
Share

add this to your Evade function

 Debug.DrawRay(obj.transform.position, direction, Color.blue);

pause the game and use the "one frame forward" button in the editor to see what happens frame by frame.

also, how do you update your chicken if it is not in collision?

is the moveVector updated anywhere else? is it possible that your normal update overwrites the moveVector in the next frame?

avatar image coder127 · Mar 15, 2015 at 01:30 PM 0
Share

I solved the issue I was having and you are right it was the moveVector being updated to move away from another object which was affecting the movement of my chickens.

Anyway it's solved now, I appreciate your assistance jinjin :)

avatar image JinJin · Mar 15, 2015 at 07:34 PM 0
Share

i am glad that you find my answer acceptable :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Have falling object exit from a collider after collision? 2 Answers

Determining rotation for collision avoidance? 3 Answers

AI - Raycast collision and follow player 1 Answer

Collision detection with raycast 1 Answer

Raycast for 2d with touch. 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