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 Mz3D · Jul 13, 2012 at 11:55 PM · transformvector3mathfbehaviourpingpong

Mathf.PingPong strange behaviour

I applied to some objects the Mathf.PingPong function. I followed the Script Reference indications but the objects behave in a strange way: when the Mathf.PingPong function starts (at a certain time) they move very rapidly towards the positive x axis. I don't know where they are going and if they are going to come back. It doesn't seem so.

This is the code:

 function Update (){
     if (Time.time > 3){
  transform.position = transform.position + Vector3(Mathf.PingPong(Time.time, 3), 0, Mathf.PingPong(Time.time, 2));
  }
 }

Where's the mistake?

Comment
Add comment · Show 1
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 Mz3D · Jul 14, 2012 at 08:59 AM 0
Share

anyone has any suggestion?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Jul 14, 2012 at 04:22 PM

Try something simple, like: transform.position = Vector3(Mathf.PingPong(Time.time, 3),0,0);`. To speed it up, use Time.time*2.

The problem is that you're adding to position. If you add 1 to x every frame, clearly the object will shoot to the right. If you add Random.Range(0,3) then the object will move left in a jerky fashion (it goes 0, 1 or 2 each frame.)

PingPong(time, 3) is just a number between 0 and 3, based on the current seconds. If you add that to your x-position, of course you go right -- slow, then fast, then slow.

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 Mz3D · Jul 16, 2012 at 08:28 PM 0
Share

if i use the code:

transform.position = Vector3($$anonymous$$athf.PingPong(Time.time, 3),0,0);

the objects rapidly jump at 0,0,0 and then they start moving normally from 0 to 3 and back. $$anonymous$$y problem is: How can i make the objects start moving from their actual position, without being moved to 0,0,0 first?

avatar image Owen-Reynolds · Jul 17, 2012 at 12:46 AM 0
Share

Ping-pong "from anywhere" is tricky to use, but there are a few answers here you can look at. The trick is that Time.time is the time from the_start of the game_. Ins$$anonymous$$d, you have to save when you start ping-ponging, and find the seconds from there.

Or, sometimes easier just to use regular math to make your x go up to 3, down to 0 ... and keep the others the same. If you aren't sure how to do that, there's lots of advice here about how to learn program$$anonymous$$g for Unity.

avatar image Mz3D · Jul 17, 2012 at 12:53 AM 0
Share

i tried to use a time value, ins$$anonymous$$d of Time.time, that starts from the ping pong movement beginning. The result is that all the objects jump to a single point in spaace and start moving all togheter

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Set speed by distance from object 1 Answer

C# Mathf.PingPong Rotate Back and Forth 2 Answers

Unity Car Accelerometer 0 Answers

using MoveObject to perform 2 translations at the same time 1 Answer

Different between transform.Translate() and Vector3.MoveTowards? 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