• 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 leonalchemist · Oct 16, 2016 at 05:55 PM · rigidbodyoffsetmovepositionpingpong

mathf.pingpong with offset

basically want to try add an offset to a moving sprite that moves back and forth between two point, for example a moving platform that moves between 0 and 3 on the x axes but I want that object to start at 1 on the x axes, not sure how to achieve it as it always starts at the pingpong's start position.

thinking it's either got something to do with the startPos variable or Time.time synchronizing the pingpong

 function Start() {
     rb2D = GetComponent.<Rigidbody2D>();
     startPos = rb2D.position;
 
     if(target.x == rb2D.position.x && speedX == 0)
     {
         target.x = 1;
     }
     if(target.y == rb2D.position.y && speedY == 0)
     {
         target.y = 1;
     }
     dif = target- rb2D.position;
     sx = Mathf.Sign(dif.x);
     sy = Mathf.Sign(dif.y);
 }
 
 function Update () {
     rb2D.MovePosition(Vector2(startPos.x + Mathf.PingPong(Time.time * speedX, dif.x*sx)*sx, startPos.y + Mathf.PingPong(Time.time * speedY, dif.y*sy)*sy));
 }


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

Answer by leonalchemist · Oct 17, 2016 at 04:32 PM

erm... i don't know, I've tried about 100 different things now and none really working, currently have

 rb2D.MovePosition(Vector2(startPos.x + offset.x + Mathf.PingPong(Time.time * speedX, dif.x*sx-offset.x)*sx, startPos.y + offset.y + Mathf.PingPong(Time.time * speedY, dif.y*sy-offset.y)*sy));

either the object doesn't move where I want it to or it synchronizes with the one with no offset.. quite lost

Comment
Add comment · Show 5 · 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 Raresh · Oct 17, 2016 at 04:40 PM 0
Share

Ok, I have to ask you one more thing. Why are you multiplying Pingpong by sx again? You'll obtain values between 0 and your target ^2. PingPong returns a value from 0 to sx, you don't need to multiply it again by sx. It doesn't return a value from 0...1. Unless that's what you're looking for?

avatar image Raresh Raresh · Oct 17, 2016 at 04:41 PM 0
Share
 rb2D.$$anonymous$$ovePosition(Vector2(startPos.x + offset.x + $$anonymous$$athf.PingPong(Time.time * speedX, dif.x*sx-offset.x), startPos.y + offset.y + $$anonymous$$athf.PingPong(Time.time * speedY, dif.y*sy-offset.y)));

^ should ping-pong your object from startpos + offset to target.

avatar image leonalchemist Raresh · Oct 17, 2016 at 04:52 PM 0
Share

don't understand every detail of it, got some help for the ping pong, works well without any offset; and tried that code but still has couple issues..

Show more comments
avatar image leonalchemist Raresh · Oct 17, 2016 at 05:19 PM 0
Share

but it works well though, whether the target value is smaller or bigger than startPos as well

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

70 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

Related Questions

Oscillate at offset position...? 1 Answer

rigidbody.Moveposition doesnt make my player move diagonally. 1 Answer

Player movement jittery when hitting walls and using Rigidbody.MovePosition. 1 Answer

Is it possible to prevent parent rigidbody physics from effecting children rigidbodies? 1 Answer

Move toward an object 2 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