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

2 Replies

  • Sort: 
avatar image
0
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
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 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
1

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

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

AI Field of vision 1 Answer

AI script works, when we add a blocked function it stops working HELP 0 Answers

Need Help making Pong AI beatable 1 Answer

Collider Vision AI question. Solved! 0 Answers

AI raycasting problem 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