• 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 /
This question was closed Jul 14, 2012 at 12:22 AM by TheDarkVoid for the following reason:

The question is answered, right answer was accepted

avatar image
Question by TheDarkVoid · Jun 24, 2012 at 07:44 PM · c#raycastai

[C#] Raycast based AI

i have this code

 Vector3 fwd = transform.InverseTransformDirection(Vector3.forward);
 Vector3 left = transform.InverseTransformDirection(new Vector3(-ViewAngle, 0, 1));
 Vector3 right = transform.InverseTransformDirection(new Vector3(ViewAngle, 0 , 1));

 bool CanMove;
 if(Physics.Raycast(transform.position, fwd, out hit, StayD))
     CanMove = false;
 else if(Physics.Raycast(transform.position, left, out lhit, StayD-5) || Physics.Raycast(transform.position, right, out rhit, StayD-5))
     CanMove = false;
 else
     CanMove = true;
 if(CanMove)
     transform.Translate(Vector3.forward * Time.deltaTime * MoveSpeed);

When i run the code, the ai keeps going even though there is something infront of it, the code worked fine when there was no test for if there is something on it's forward left and forward right, what can i do to make this work, i need the ai to be able to see like this:

alt text

the green is where can be seen. i would like for the view angle to be adjustable. any suggestions, you can look at the full code here: AiCode

Ai Diagram.png (23.4 kB)
Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by TheDarkVoid · Jun 24, 2012 at 11:23 PM

This:

 Vector3 fwd = transform.InverseTransformDirection(Vector3.forward);
 Vector3 left = transform.InverseTransformDirection(new Vector3(-ViewAngle, 0, 1));
 Vector3 right = transform.InverseTransformDirection(new Vector3(ViewAngle, 0 , 1));

Should Be This:

 Vector3 fwd = transform.TransformDirection(Vector3.forward);
 Vector3 left = transform.TransformDirection(new Vector3(-ViewAngle, 0, 1));
 Vector3 right = transform.TransformDirection(new Vector3(ViewAngle, 0 , 1));
Comment

People who like this

0 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 Berenger · Jun 25, 2012 at 12:20 AM 0
Share

That's what I meant with transform.forward. However, your left and right vector are turned forward apparently, so you do need TransformDirection. Could rotate forward, but your method works too.

avatar image

Answer by Berenger · Jun 24, 2012 at 08:06 PM

Use Debug.DrawRay to check where the rays are cast. Make sure the distance is sufficient to hit the obstacles. You can use transform.forward instead of InverseTransformDirection.

Comment
TheDarkVoid

People who like this

1 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 TheDarkVoid · Jun 24, 2012 at 08:19 PM 0
Share

i did, it draws the rays, and they are sufficient distance, but it just keeps moving, sometimes is stops and wait like it's suppose to but most of the time it just keeps going.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

For each Raycast? 1 Answer

Distribute terrain in zones 3 Answers

How can I get a character to patrol and follow terrain? 1 Answer

How to make "enemy" have a frustum(cone) view and not a circle radius 2 Answers

Raycast & AI problem. 1 Answer


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