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
1
Question by ColinBurke · Sep 30, 2012 at 08:28 PM · lerpvector2

Limit Lerp Distance From Target

I am currently creating an 2-d orb game, where the goal is to fire/"sling" the orb into the goal. This is the method that allows the user to pull back the orb (just before the player releases the mouse button, and the Fire() method is called):

     void PullBackOrb(Vector2 mousePos)
     {
         if(Vector2.Distance(firePos, mousePos) < 3)
         {
             print("Orb exceeded max pullback distance");

             //Limit Pullback distance (?)
 
         }
         Vector2 lerpPos = Vector2.Lerp(transform.position, mousePos, 10 * Time.deltaTime);
         transform.position = lerpPos;
     }


Is there a way to limit the distance the user can pull back the orb; if the user pulls the orb beyond a radius of 3 from the firePos; so even if the mouse is beyond the radius of 3, the orb will act like it must stay within the radius?

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 aldonaletto · Sep 30, 2012 at 09:11 PM

You could limit the distance mousePos-firePos like this:

 void PullBackOrb(Vector2 mousePos){
     if (Vector2.Distance(firePos, mousePos) > 3){
         // find the mousePos offset from firePos:
         Vector2 offset = mousePos - firePos; 
         // clamp the offset distance to 3:
         offset = Vector2.ClampMagnitude(offset, 3);
         // adjust mousePos to this distance:
         mousePos = firePos + offset;
     }
     Vector2 lerpPos = Vector2.Lerp(transform.position, mousePos, 10 * Time.deltaTime);
     transform.position = lerpPos;
 }

This code follows the mouse pointer direction, but limits the orb position to a distance 3 from firePos.

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 ColinBurke · Sep 30, 2012 at 09:19 PM 0
Share

Wow! I can't believe I didn't think to do it that way. I guess manually subtracting two vectors gives you a workable magnitude. I was trying to clamp the Vector2.Distance of those, and rework that in. It was a mess.

Thanks!

avatar image aldonaletto · Oct 01, 2012 at 01:51 AM 0
Share

We're actually subtracting two points, which produces a vector. This vector's magnitude is the distance between the two points, and the very handy function Vector2.Clamp$$anonymous$$agnitude can clamp it to the desired value without affecting the vector direction.

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

11 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

Related Questions

[C#] can someone give me a detailed explanation of lerp 1 Answer

Vector3.Lerp result in "laggy" movement while running on iOS devices 2 Answers

Looking to see if the method I'm using for movement is correct or if I'm doing something wrong. 0 Answers

Difficulty with Vector2 Lerp, object jumping, not moving 2 Answers

Moving an UI element from point A to point B 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