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 Tracey P · Oct 06, 2011 at 11:43 AM · raycastairaycastingenemies

Raycasting and enemies ai

Im trying to script my enemies ai so that when it gets within attacking range of a player placed barrier then it will start to attack. I think I need to use raycast for this. If someone could point me toward a tutorial that would be very helpfull thank you everyone.

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

· Add your reply
  • Sort: 
avatar image
0

Answer by timsk · Oct 06, 2011 at 12:01 PM

You mention raycasting in your question, but another way to do it would be to simply check the distance between the 2 objects, if its above the enemies attack range, move towards the barrier, if not start attacking. Something like this would work:

 distance = Vector3.Distance(transform.position, barrier.transform.position); 
 //checks the distance between the 2 objects given in the paranthesis
 
 if(distance>=range)    
 {    
     MoveTowards();
     RotateTowards();
 }
 else
 {
 AttackBarrier();
 }

Hope this helps :).

Also, i hope someone posts an answer using raycasting, would be interesting to see.

Comment
Add comment · Show 2 · 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 Tracey P · Oct 09, 2011 at 03:29 AM 0
Share

Well i got this in the script but its not working properly. The monster moves toword your charachter but when he is in range he dosnt start to attack.

avatar image timsk · Oct 10, 2011 at 06:22 AM 0
Share

thats probably a problem with your attack function. Post a seperate question for it.

avatar image
0

Answer by Owen-Reynolds · Oct 06, 2011 at 03:54 PM

You pretty much always want to use distance, above, to check whether you attack. There are two more things you may want to check:

1) Is the target within my "view cone"? - can we see things behind us. Check using Quaternion.Angle

2) Do I have a clear line of sight to it? - it's nearby and in front of me, but a giant wall is between us. This is what rayCasting is used for.

Raycasting isn't difficult, but has some tricks. For example, you have to be sure to look from your eyes to the target -- not from your feet. It you want to see partially blocked targets, you need two (or more) raycasts "can I see his left side? No, well can I see his right side? No, well can I see his head?" Your friends also block your view, so when Bob ahead of you shouts kill and starts running, you say "huh -- I don't see anything." You can use layers to see through friends.

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 Tracey P · Oct 09, 2011 at 02:35 AM 0
Share

Ok sorry i didnt mention this but its 2D but this should really help me visualize it and can you point me twords a tut for raycast scripting

avatar image syclamoth · Oct 09, 2011 at 05:17 AM 0
Share

Characters in 2D games still have eyes- only the player has the 'God's eye view' thing happening.

(except when the characters don't have eyes. Because they're weird slimes or something)

avatar image Tracey P · Oct 10, 2011 at 01:34 AM 0
Share

...well when you mention it the main enemies are you guessed it SLI$$anonymous$$ES

avatar image syclamoth · Oct 10, 2011 at 01:36 AM 0
Share

Well, I guess they still have a sense of smell? Huh. Well that shoots my analogy down...

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

AI Raycast problem 1 Answer

Raycasting crashing Unity? 3 Answers

Problem with Unity Ray Trace AI Detect Player 0 Answers

Another Raycast Issue. 0 Answers

Finding RayCastHit's Origin Position 2 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