• 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 cariaga · Feb 16, 2014 at 03:22 PM · mathfpingpong

Int Based PingPong or oscillation without using Mathf.Pingpong?

 (int)start+Mathf.PingPong(Time.time, end)

something similar to this without casting float to int. but use an int oscillation instead is their a direct approach to this

Comment
Add comment · Show 4
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 robertbu · Feb 16, 2014 at 04:54 PM 1
Share

I don't know why you are trying to avoid the cast, but I believe the result you are looking for is something that counts up to some end and then back down to 0 at a fixed rate. A few lines in a co-routine or a function called by InvokeRepeating() might work.

avatar image cariaga · Feb 16, 2014 at 05:21 PM 0
Share

thank you for your suggestion here is actually what i did :) how stupid of me... if(i<=end && CountUp==true){ i++; if(i==end){ CountUp=false; } }if(i>=start && CountUp==false){ i--; if(i==start){ CountUp=true; } }

if their is a much simplier version of it i would be glad to know

avatar image robertbu · Feb 16, 2014 at 05:53 PM 0
Share

Not necessarily better, but I would have oscillated the value that is being added or subtracted.

 #pragma strict
 
 private var val = 0;
 private var dir = 1;
 
 var period = 1.0;
 var end = 5;
 
 function Start() {
     InvokeRepeating("PingPongInt", period, period);
 }
 
 function PingPongInt() {
     val += dir;
     if (val == 0) dir   = 1;
     if (val == end) dir = -1;
 } 
avatar image cariaga · Feb 19, 2014 at 10:32 AM 0
Share

Please Change it to answer so i have an alternative :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by cariaga · Feb 16, 2014 at 05:43 PM

 if(i<=end && CountUp==true){
  i++;
 if(i==end){
  CountUp=false;
  }
  }
 if(i>=start && CountUp==false){
  i--; if(i==start){
  CountUp=true; 
 } 
 }
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 flamy · Feb 19, 2014 at 10:37 AM 0
Share

not the best way..

avatar image cariaga · Sep 20, 2014 at 08:39 PM 0
Share

well it worked on my specific goal :) robertbu answer is also good

avatar image
1

Answer by Anderson-Cardoso859 · Nov 20, 2016 at 09:56 AM

 public static int PingPong(int t, int length)
 {
       int q = t / length;
       int r = t % length;
 
        if ((q % 2) == 0)
             return r;
         else
             return length - r;
 }
Comment
Add comment · Show 1 · 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 Bunny83 · Nov 20, 2016 at 12:21 PM 0
Share

It would be easier to simply use double the length, do a single modulo and a conditional return value for the first half and one for the second half.

 int r = t % (2*length);
 if (r < length)
    return r;
 else
    return length*2 - r;

Though your solution does work as well. so +1.

Note for potential users of this method: The range of the returned values will be 0 - length inclusive. So PingPong(xxx, 5) will generate the sequence:

 0  1  2  3  4  5  4  3  2  1  0  1  2  3  4  5  4  3  2  1  0  ...

So it effectively "swings" over 6 values. I'm just adding this as most int-based methods have the max value exclusive like Random.Range for example.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Range repeat? like 50 to 100 then back to 50 1 Answer

Mathf.Pingpong without the repetition!? 1 Answer

Mathf.PingPong strange behaviour 1 Answer

C# Mathf.PingPong Rotate Back and Forth 2 Answers

Platforms Oscillating with Mathf.PingPong not working properly 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