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
1
Question by leonalchemist · Oct 10, 2016 at 09:09 PM · mathfpingpong

using Mathf.PingPong

trying to move a 2d sprite using Mathf.PingPong, could be a stupid question, just wondering how to go about it when the starting position is lower than the target position. Example is if I want a trap to start at 9 on the y-axis and go to 6 on the y-axis, the code would be like this:

 rb2D.MovePosition(Vector2(rb2D.position.x, Mathf.PingPong(Time.time * speed, startPos.y-targetPos.y) + targetPos.y));

but if I want it to start at 6 and go to 9 then not sure how to make this work?

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 Bunny83 · Oct 10, 2016 at 09:45 PM

So you just want a general solution for both cases? The easiest way is to use Lerp as well as PingPong. However when using lerp the speed becomes distance dependent as the time to reach the target is always the same. You can however correct it by the distance between start and end:

 // UnityScript / C#
 var dif = targetPos - startPos;
 var t = Mathf.PingPong(Time.time * speed / dif.magnitude, 1f);
 rb2D.MovePosition(Vector2.Lerp(startPos, targetPos, t));

Another way would be to take the absolute difference which you use inside the pingpong and in addition use the sign of the difference to move in the correct direction.

 // UnityScript
 var dif = targetPos - startPos;
 var s = Mathf.Sign(dif.y);
 rb2D.MovePosition(Vector2(rb2D.position.x, startPos.y + Mathf.PingPong(Time.time * speed, dif.y*s)*s));

Here dif.y*s is simply the absolute value. So if it's "-7" it will be "7" since (-7)*(-1) --> 7. So the pingpong is done in the positive range and we simply multiply the result again by "s" to get either a negative or positive offset depending on the direction.

The first Lerp version however is much easier and also works in any direction, not just the y axis.

Comment
Add comment · Show 8 · 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 leonalchemist · Oct 10, 2016 at 10:13 PM 0
Share

thanks, playing with it a bit but looks like it's doing the job well, thanks :)

avatar image leonalchemist leonalchemist · Oct 10, 2016 at 10:54 PM 0
Share

umm, also trying to separate the time for x and y axes, I'm assu$$anonymous$$g you can have 2 different t variables but not quite sure how to use them in the end result... feels like it shouldn't be that difficult :X

avatar image Bunny83 leonalchemist · Oct 10, 2016 at 11:07 PM 0
Share

Why would you seperate the movement on the x and y axis? This would result in a more or less chaotic movement since when the two motions aren't synchronised you would move anywhere inside the rectangle between your $$anonymous$$ and max x / y values. The movement would be like a bouncing ball that is reflected on the border of that rectangle.

If you really want to do this you can't use Lerp. Lerp is a linear interpolation. So you have two points and you go from one to the other in a straight line (linear). So you would need to use the other approach where you move on each axis seperately.

Show more comments

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

58 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

Related Questions

Help with Mathf.PingPong 2 Answers

Does Mathf.PingPong always have to start at (0,0,0)? 1 Answer

How to rotate object back and forth on input hold? 1 Answer

Mathf function on GetKey 1 Answer

Mathf.Pingpong without the repetition!? 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