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 Totalywicked · Nov 01, 2011 at 10:50 PM · aispeeddistanceautomation

Speed based on Distance

Speed based on Distance… I am working on a side scrolled Ai. What I want to do is as Ai get closer to player he will slow down. As of right now I am using few IF statement.

Is there a way to make it more automatic?? Maybe few ideas on how to do it???

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

3 Replies

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

Answer by Fabkins · Nov 02, 2011 at 12:26 AM

Comments seemed to have scrambled the format so putting it here instead with some extra comments.

 var dist=Vector3.Distance(me.position, ai.position);
 
 if ( dist > MaximumDistance) 
 { 
     // AI is far away give him Max speed
     aiSpeed = MaxSpeed; 
 } 
 else if ( dist < MinimumDistance) 
 { 
     // AI is closer than minimum distance give him Min speed
     aiSpeed = MinSpeed; 
 } 
 else 
 { 
     // AI is between Max/Min distance so give him a speed proportional 
     // between Min and Max speed
 
     // This is the % ratio between Max and Min distance
 
     var distRatio=(dist - MinimumDistance)/(MaximumDistance - MinimumDistance);
 
     // This is the extra speed above min speed he can go up too
 
     var diffSpeed = MaxSpeed - MinSpeed;
 
     // Final calc 
     aispeed = ( distRatio * diffSpeed) + MinSpeed; 
 }
 
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 Dracorat · Jun 21, 2013 at 06:13 PM 1
Share

For the record, there is a term for this type of behavior. It's called "arrival". You can google a lot of vector movement formulae for your various use cases. This would be "vector movement arrival" and is likely to lead you to pages that list a bunch of other types of behaviors.

avatar image araz01 · Feb 24, 2020 at 11:42 PM 0
Share

what language is this? what are you using? latin?

avatar image
0

Answer by Fabkins · Nov 01, 2011 at 11:01 PM

This what I would do. Assuming that beyond a given distance you want the AI to go at maximum speed and grind to a halt when he reaches you I do something like:

 var dist=Vector3.Distance(me.position, ai.position);
 
 if ( dist > MaximumDistance)
 {
  aiSpeed = MaxSpeed;
 }
 else
 {
  aiSpeed = ( dist / MaximumDistance ) * MaxSpeed;
 }
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 Totalywicked · Nov 01, 2011 at 11:54 PM 0
Share

Thanks....

i was thinking more alon the lines of $$anonymous$$ax distance= speed 100% $$anonymous$$in distance = speed 25%

avatar image Fabkins · Nov 02, 2011 at 12:15 AM 0
Share

var dist=Vector3.Distance(me.position, ai.position);

if ( dist > $$anonymous$$aximumDistance) { aiSpeed = $$anonymous$$axSpeed; } else if ( dist < $$anonymous$$inimumDistance) { aiSpeed = $$anonymous$$inSpeed; } else { // This is the % ratio between $$anonymous$$ax and $$anonymous$$in distance

var distRatio=(dist - $$anonymous$$inimumDistance)/($$anonymous$$aximumDistance - $$anonymous$$inimumDistance);

// This is the extra speed above $$anonymous$$ speed he can go up too

var extraSpeed = $$anonymous$$axSpeed - $$anonymous$$inSpeed;

// Final calc aispeed = ( distRatio * extraSpeed) + $$anonymous$$inSpeed; }

avatar image Totalywicked · Nov 02, 2011 at 12:16 AM 0
Share

Thank you sir

avatar image Fabkins · Nov 02, 2011 at 09:09 PM 0
Share

If this answered your question please mark it answered.

avatar image
0

Answer by Diekeke · Jun 21, 2013 at 06:11 PM

Here's a faster way to do it, just get the distance between AI and Player, and make the speed equal to it (aiSpeed = dist), this way, the AI will move towards the player gradually from fast to slow until it reaches the player, and if you want that the AI moves from slow to fast, just make the speed equals the max distance you get minus the distance, example: aiSpeed = 50 - dist So there you have it, hope this helps you! :D

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

6 People are following this question.

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

Related Questions

Following the next item in an array 0 Answers

Do something every each multiple of 1000.. 2 Answers

Need help with basic AI script. 2 Answers

Help! How do I prevent a follower from turning heel and running away? 0 Answers

focusing on a unit during unit detection in an RTS 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