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
14
Question by SisterKy · Jul 29, 2011 at 08:26 PM · timemathfpingpong

what exactly does Time.time do in Mathf.PingPong?

I'm sorry for the stupid question but I can't seem to get my head around this one.

Whenever I see PingPong used, I see it with Time.time, but I don't understand how it works exactly.

From the Scripting-Reference:

Mathf.PingPong
static function PingPong (t : float, length : float) : float

PingPongs the value t, so that it is never larger than length and never smaller than 0.
The returned value will move back and forth between 0 and length.


function Update () {
    // Set the x position to loop between 0 and 3
    transform.position = Vector3(
       Mathf.PingPong(Time.time, 3), transform.position.y, transform.position.z);
} 

So...
Mathf.PingPong PingPongs value t (=Time.time), so that it is never larger than length and never smaller than 0.
How does it work, that it PingPongs 'value t'??
Time.time is read-only. Time.time is an ever-increasing value dependent on timescale and seconds-since-game-started.
So how can Time.time be 'PingPonged' to be never larger than length and never smaller than 0?

If it's clear that Mathf.PingPong returns a value between 0 and lenght, the 'value t' must be to somehow set the speed, but why do we want this speed to be an ever-increasing float. And why doesn't this ever-increasing float make the speed actually... you know... increase?? O-o

I have a nebulous hunch, that PingPong probably needs a constantly increasing value to work (though I don't get why...)
So could I also use something like this?:


var counter : float = 1.0;
function Update () {
  counter += 0.2;
  transform.position = Vector3 (Mathf.PingPong(counter, 4), tr.pos.y, tr.pos.z);
}

Can someone explain to me why PingPong works in such a weird way and doesn't look more like
static function PingPong (startvalue:float, endvalue:float, speed:float): float
which would have the convenient side-effect that we don't have to subtract something if we want to ping-pong around 0?

Greetz, Ky.

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

Answer by Graham-Dunnett · Jul 29, 2011 at 09:26 PM

1) L = 2 * length

2) T = t modulo L

3) x = L - T

4) return x

As t increase from 0 to length return t. If t is in the range length to 2*length return 2*length-t. This will change from length back to zero. The modulo operation essentially repeatedly subtracts 2*length until the result is in the range zero to 2*length.

Just try it with some numerical values.

To answer your question, time is never changed. The ping-ponged value is returned by the function.

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 SisterKy · Jul 30, 2011 at 07:53 AM 0
Share

thank you for the accurate answer... took me a few times to read, but I think I got it now... :) Greetz, $$anonymous$$y.

avatar image bandtank · Sep 17, 2014 at 02:25 PM 0
Share

As t increase...

You mean T, not t. The way you've written it is confusing because the capital letter matters given your choice of variables. I get incorrect values if I use t ins$$anonymous$$d of T.

This post really should show the conditional statement in the numeric list as well. Something like this:

 L = 2 * length
 T = Time.time mod L
 
 if 0 <= T < length:
     return T
 else:
     return L - T

I can't figure out why this comment has a 5. on one of the lines. I've tried everything I can think of to remove it to no avail.

avatar image
12

Answer by Owen-Reynolds · Jul 29, 2011 at 11:26 PM

Yes, any increasing value works for ping-pong. Time.time is the easiest, but if you want to start ping-ponging after a certain event, than using Time.time will snap you to a randomish spot (if you start doing a length 4 ping-pong at time 7, you'll jump to 1 unit away, coming back to the start.) In that case, your counter method is exactly what you want.

The version of ping-pong you're thinking of is a fire-and-forget. It could be written in a coroutine with yield. Yes, that would be more useful, but coroutines confuse a lot of people and they are tricky to stop. That's probably why iTween is so popular.

PingPong is a simple 1-output math function. Not great, but better than nothing. You give it inputs, it gives you an output. If no inputs change, the output doesn't change. Take a look at Mathf.SmoothDamp. It ramps your speed up, moving you, then coasts down to the end. You still have to call it every frame with an increasing value. You also have to create the currentVelocity variable, start it at 0, and pass it in by reference. SmoothDamp changes it, but the only way it "remembers" it for next time is because you pass it back in.

PingPong could have been written as with a start besides 0, as you suggest: PingPong(time, start, stop). Think of pingPong like Mathf.Abs. You could just use an if -- Abs is just a tiny bit nicer.

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 SisterKy · Jul 30, 2011 at 07:53 AM 0
Share

Thank you for the follow up on Graham's answer. Clearing this up quite a bit more =) Greetz, $$anonymous$$y.

avatar image sdclark79 · Jun 13, 2017 at 03:26 AM 0
Share

Thanks for the explanation, very insightful

avatar image
0

Answer by cgkaransahu · Jun 05, 2020 at 02:08 PM

var position = new vector3 ( 0, 0, Mathf.PingPong( (SpeedValue*Time.time),EndPos ) ); // This Will Run In Z Axis Only Between 0, Endpos;

//But if you want Custom Minimum Value like = 5, -3, etc. //If you want negative Number then minus that number like this = var position = new vector3 ( 0, 0, Mathf.PingPong( (SpeedValue*Time.time), ( EndPos + MinValue ) ) - MinValue);

//If you want positive Number then Plus that number like this = var position = new vector3 ( 0, 0, Mathf.PingPong( (SpeedValue*Time.time), ( EndPos - MinValue ) ) + MinValue);

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

Pausing a Mathf.PingPong at the ends 0 Answers

Separate Numbers ? 1 Answer

My enemy cube moves through walls 0 Answers

Mathf.SmoothStep sequence Time problem 1 Answer

how to rotate a gameobject between two angles front and back direction? 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