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 ThatUnityNoob · Apr 09, 2012 at 09:10 PM · raydetecting

AI Detecting Object (Raycast)

Hi again!

I'm still working on trying to get my AI to get around object around the map without running through them. What I need is for my AI to send out rays in the 180 degree radius in front of him. And if one of those rays hit an object that is say 50 units away from him...He will rotate little by little until that ray isn't hitting anything again. This would be very helpful for my AI when he is chasing my player around and my player runs behide a wall. Also when the AI is just wondering around freely...What I have so far is one ray sent out in front of the AI that picks up objects up to 50 units away...I don't know where to go from there on rotating the AI or sending out more then just one ray...The script is below and if anyone needs any other script to help salve this problem just ask and I would be happy to get it to them...Any help would be greatly appreshated!!! Thanks!!!

function Update () {

var fwd = transform.TransformDirection (Vector3.forward);

if (Physics.Raycast (transform.position, fwd, 50)) {

 print ("There is something in front of the object!");  
 }
 }
Comment
Add comment · Show 21
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 AlucardJay · Apr 10, 2012 at 02:16 AM 0
Share

there is some information on this link that you may find useful :

http://answers.unity3d.com/questions/235017/enemy-ai-avoiding-collision.html

avatar image ThatUnityNoob · Apr 10, 2012 at 02:46 AM 0
Share

I saw this today as I was searching for this answer!!! It seems very useful be the coding looks really forign to me...any ideas? Thanks

avatar image ThatUnityNoob · Apr 10, 2012 at 02:51 PM 0
Share

Ok I got almost all of it after looking back at the code u posted...Thanks!!! Now all I need is for the AI to dectect which path is the shortes. Like if there is a wall and hes to the very right it of it...go right...not left...Any ideas for that? Thanks!!!

avatar image AlucardJay · Apr 10, 2012 at 02:58 PM 0
Share

sorry i havn't replied, been busy. Also I am not very good with rotations, but here is a video with some more information that might help. Even if you follow the video, write the script , and see how this works. Obstacle Avoidance - Unity 3d link : http://vimeo.com/9304844

avatar image AlucardJay · Apr 10, 2012 at 03:07 PM 0
Share

Here is the script to help you along (I have changed it when I was testing my own ideas , but it should be almost the same. I tested This script and it works. Just set your target vector.) :

 // Avoid Obstacle Script
 // ---------------------
 
 var speed : float = 10.0;
 var target : Vector3 = Vector3(0, 0, 0);
 private var dir : Vector3;
 private var dirFull : Vector3;
 
 // ----
 
 function FixedUpdate()
 {
     // -- Obstacle Avoidance Tutorial --
     
     // the directional vector to the target
     dir = (target - transform.position).normalized;
     var hit : RaycastHit;
     
     // check for forward raycast
     if (Physics.Raycast(transform.position, transform.forward, hit, 1)) // 20 is raycast distance
     {
         if (hit.transform != this.transform)
         {
             Debug.DrawLine (transform.position, hit.point, Color.white);
             
             dir += hit.normal * 20; // 20 is force to repel by
         }
     }
     
     // more raycasts    
     var leftRay = transform.position + Vector3(-0.125, 0, 0);
     var rightRay = transform.position + Vector3(0.125, 0, 0);
     
     // check for leftRay raycast
     if (Physics.Raycast(leftRay, transform.forward, hit, 1)) // 20 is raycast distance
     {
         if (hit.transform != this.transform)
         {
             Debug.DrawLine (leftRay, hit.point, Color.red);
             
             dir += hit.normal * 20; // 20 is force to repel by
         }
     }
     
     // check for rightRay raycast
     if (Physics.Raycast(rightRay, transform.forward, hit, 1)) // 20 is raycast distance
     {
         if (hit.transform != this.transform)
         {
             Debug.DrawLine (rightRay, hit.point, Color.green);
             
             dir += hit.normal * 20; // 20 is force to repel by
         }
     }
     
     // --
     
     // $$anonymous$$ovement
     
     // rotation
     var rot = Quaternion.LookRotation (dir);
         
     //print ("rot : " + rot);
     transform.rotation = Quaternion.Slerp (transform.rotation, rot, Time.deltaTime);
     
     //position
     transform.position += transform.forward * (2 * Time.deltaTime); // 20 is speed
     
     // -- end tutorial --
     
 }

This works by getting the .normal of the surface the Raycast hit, and modifying the (dir) , then the gameObject looks at the position (dir), then the gameObject moves forward in the rotation it is facing. I think ... =]

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by AlucardJay · Apr 13, 2012 at 07:22 PM

See My Comments =]

Comment
Add comment · Show 1 · 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 ThatUnityNoob · Apr 22, 2012 at 08:13 PM 0
Share

YES!!! I got the AI finily to dodge objects!!! Thank you for all your help!!! One last question to finish off the AI...I can't get the AI to ignore the player for raycasting as far as trying to avoid it. The Layer thing dosn't seem to like me...any ideas? THAN$$anonymous$$S! O and also...where should I add the script were the AI avoids in corners? THAN$$anonymous$$S!!!

avatar image
0

Answer by yezzer · Apr 12, 2012 at 05:42 PM

Have you considered using either UnitySteer or if you have Pro, using the built-in pathfinding in 3.5?

Comment
Add comment · Show 1 · 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 ThatUnityNoob · Apr 12, 2012 at 07:42 PM 0
Share

Yes. I have tried UnitySteer and I do not have pro...UnitySteer isn't really what I Need and its way to compicated...Thanks though!!!

avatar image
0

Answer by MyCatFishSteve · Jan 29, 2013 at 09:53 PM

i wouldnt set the Raycast to 180 degrees, otherwise it might be hard to get through small gaps

Comment
Add comment · 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
0

Answer by sjmurray · Feb 21, 2013 at 06:26 PM

i think the suggested code above will also have problems with small gaps and also if the enemy and the player create a line that is exactly perpendicular to a wall (the enemy will actually run straight away from you). Good for small objects though.

Comment
Add comment · 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

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

9 People are following this question.

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

Related Questions

SORPG: Distance problem? 1 Answer

Confused as to how my RaycastHit script works 1 Answer

Detecting Raycast collision on an enemy to deal damage 1 Answer

How to move a player to a certain point? 0 Answers

Upper limits of Raycasting; Physics engine "gives up" 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