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 /
avatar image
0
Question by JUSTCAMH · Jan 31, 2019 at 10:00 PM · mathmathf.lerp

Lerp that wraps around a variable x

The 'Mathf.LerpAngle' function is incredibly useful. This is like a lerp, but it wraps correctly around 360. How would I create a lerp that wraps correctly around a variable x? I have a racing game, where the course consists of frequent worldpoints. These points have indexes ranging from 0 to x, and I need a lerp that would appropriately take the shortest distance to any given point. Therefore, if I'm at (x-1) and want to go to 0, the lerp would go to x then zero, rather than (x-2), (x-3)... As bonus points, could an answer also include a version of this for MoveTowards?

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

Answer by Ymrasu · Jan 31, 2019 at 11:45 PM

If you have an 1D axis (your variable x) that repeats/loops/wraps-around you can think of as a circle. We know that if the distance from A to B on a circle is more than half the circumference, it means that you went the wrong way and that going to other way is shorter which. Lets say you have indexes from 0 to 9 (length of 10).If you want to go from 2 to 8; if would count 2,3,4,5,6,7,8, you get a distance of 6.This is more than half the length (6/10 = 60%) so we know going the other directionis shorter, 2,1,0,9,8 for a distance of 4. This can also be done by doing length - distance (10 - 6 = 4).

 // from a to b
 int a = 2;
 int b = 8;
 int length = 10; // length can be array.lenth if you keep the points in an array
 
 int distance = Mathf.Abs(a - b); // distance is 6 in this case
 
 // then we can check if it is more than half way
 if (distance > (float)length * 0.5f) {
     distance = length - distance;
 }
 
 // now that we have our shortest distance
 // which way do we travel?
 int dir = 0;
 // we can check if traveling positive is correct
 if (a + distance == b) {
     // it is! make the dir positive
     dir = 1;
 } else {
     // nope the direction is neg
     dir = -1;
 }
 
 // with the direction and distance figured out, we can lerp!
 int x = Mathf.RoundToInt(Mathf.Lerp(a, a + (distance*dir), time));
 
 // then we wrap around 0 if needed
 if (x < 0) {
     x += length;
 } else if (x > length) {
     x -= length;
 }


This will make x lerp from a to b taking the shortest distance wraping around 0-length. You will need to figure out where to put all this, you only need to check the distance once. I did not test any of this code, there may be typos.

Comment
Add comment · Show 3 · 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 JUSTCAMH · Jan 31, 2019 at 11:58 PM 0
Share

Awesome solution, paired with a great explanation. Thank you for the answer!

avatar image Ymrasu JUSTCAMH · Feb 01, 2019 at 12:26 AM 0
Share

I was curious about using movetowards to wrap around the world so I came up with this:

// I had my player at (2,0,2) and targetPos at (8,0,8) public Vector3 targetPos; public float length = 10f; public float width = 10f;

     private void $$anonymous$$oveWrapAroundWorld()
     {
         Vector3 pos = transform.position;
         Vector3 dist = new Vector3($$anonymous$$athf.Abs(pos.x - targetPos.x), 0, $$anonymous$$athf.Abs(pos.z - targetPos.z));
 
         if (dist.x > length * 0.5f) {
             dist.x = length - dist.x;
         }
 
         if (dist.z > width * 0.5f) {
             dist.z = width - dist.z;
         }
 
         Vector3 dir;
         if (pos.x + dist.x == targetPos.x) {
             dir.x = 1;
         } else {
             dir.x = -1;
         }
         if (pos.z + dist.z == targetPos.z) {
             dir.z = 1;
         } else {
             dir.z = -1;
         }
 
         Vector3 newTargetPos = new Vector3(pos.x + (dist.x * dir.x), 0, pos.z + (dist.z * dir.z));
         pos = Vector3.$$anonymous$$oveTowards(pos, newTargetPos, 1f * Time.deltaTime); // put in your speed variables
 
         if (pos.x < 0) {
             pos.x += length;
         } else if (pos.x > length) {
             pos.x -= length;
         }
 
         if (pos.z < 0) {
             pos.z += width;
         } else if (pos.z > width) {
             pos.z -= width;
         }
 
         transform.position = pos;
     }
 
     void Update()
     {
         $$anonymous$$oveWrapAroundWorld();
     }
avatar image JUSTCAMH Ymrasu · Feb 01, 2019 at 12:28 AM 0
Share

Wow! Double thanks!

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

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

Unable to fully rotate GameObject on tap 2 Answers

Mathf.Lerp like User Define Function 2 Answers

Mathf repeat at the end? help! 1 Answer

Line drawing: How can I interpolate between points to create a smooth arc? 4 Answers

Simple math always returns 0 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