Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by kidne · Apr 26, 2016 at 06:08 PM · physicstransformvector3

Detecting Distance from Object at any polint

I'm terrible with math-related coding, so I could use some help on this. Currently my enemy AI detects players or objects it can fight using colliders, the problem is that enemy attempts to fight something in it's collider without actually moving in position toward the object it should be fighting. If I have the enemy move to the gameobject it should fight, it will stand right on top of the object of course.

I'd like to be able to set a range of where the enemy should stand before attempting to attack, but I'm unsure how to do this.

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
1
Best Answer

Answer by Pharaoh_ · Apr 26, 2016 at 06:21 PM

You need to give your unit some form of an attackDistance property. This will also indicate whether this object is ranged or melee. When your OnTriggerEnter detects an incoming collider, check whether this is an enemy. Typically, attackRadius > attackDistance for melee. That said, a melee AI will become aggressive to an enemy once it enters e.g. 50 distance (attackRadius), but as a melee, the distance it will actually fight is 5 (attackDistance).

Thus, once an enemy collider enters the attackRadius, check the Vector3.Distance between the positions of the colliders' transforms. This will return a float which you can compare to your attackDistance. If the found value is greater than attackDistance, then fire an IEnumerator with a while loop; in the while condition, keep checking if the distance between your two colliders is greater than attackDistance. Once it becomes equal, the loop will exit, which is when you can launch your attack (I assume a method you call). As this is AI, the attack method could be launched by means of InvokeRepeating. Inside of this method, keep checking for the distance and if it becomes greater than attackDistance, rerun the coroutine and CancelInvoke of the attack method.

This is not the only solution; it is heavily influenced by your development design, but this is how I would approach it.

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 kidne · Apr 26, 2016 at 06:29 PM 0
Share

Vector3.Distance! That is exactly what I needed! You are a godsend, thank you so much.

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

66 People are following this question.

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

Related Questions

Why doesn't my asteroid field perfectly move with me? 1 Answer

Terrain height -> loosing Players GO childs 0 Answers

Rotating an object with VR controller 1 Answer

Why is this causing weird stutter/jitter? 0 Answers

Physics in car game: How to add drag on car from calculated resistances in Vector3 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