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 /
This question was closed Jun 10, 2019 at 10:53 AM by ratchetunity for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by ratchetunity · Jun 10, 2019 at 09:45 AM · positionupdatelerpmathfpingpong

Ping pong position using lerp

Hi!

I'm trying to make some objects move from position A to position B and then back to position A. I'm using lerp with ping pong to achive that, but it doesn't work as the objects continue moving and never get back to position A. This is my code:

     private float positionDisplacement;

     private void Start()
     {
         positionDisplacement = Random.Range(-50f, 50f);
     }

     private void Update()
     {
         transform.position = new Vector3(Mathf.Lerp(transform.position.x, transform.position.x + positionDisplacement, Mathf.PingPong(Time.deltaTime, 1f)),
             Mathf.Lerp(transform.position.y, transform.position.y + positionDisplacement, Mathf.PingPong(Time.deltaTime, 1f)),
             Mathf.Lerp(transform.position.z, transform.position.z + positionDisplacement, Mathf.PingPong(Time.deltaTime, 1f)));
     }
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

  • Sort: 
avatar image
4
Best Answer

Answer by taoria123 · Jun 10, 2019 at 10:31 AM

yes it will not come back since the the transform.position.x/y/z is changed . consider record your original position at first then do the ping pong.The following code should work.

     private Vector3 positionDisplacement;
     private Vector3 positionOrigin;
     private float _timePassed;
     private void Start(){
         float randomDistance = Random.Range(-50f, 50f);
         positionDisplacement = new Vector3(randomDistance,randomDistance,randomDistance);
         positionOrigin = transform.position;
     }
 
     private void Update(){
         _timePassed += Time.deltaTime;
         transform.position = Vector3.Lerp(positionOrigin, positionOrigin + positionDisplacement,
             Mathf.PingPong(_timePassed, 1));
     }
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 ratchetunity · Jun 10, 2019 at 10:52 AM 0
Share

Thank you @taoria123 !

avatar image
0

Answer by mansoor090 · Jun 10, 2019 at 10:23 AM

use liner equation with time. B(t) = P0 + t(P1 – P0) = (1-t) P0 + tP

for example;

 float _time = 0;
 Transform PointA:
 Transform PointB:
 void update(){
 
 _time += time.deltatime;
 
 transform.position = GetNewPosition(_time);
 
 }
 
 Vector3 GetNewPosition(float t){
 somevector = PointA + t(PointB – PointA) = (1-t) PointA + tPointB
 return somevector    

}

this may not be error free. but this is how i did it

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
avatar image
0

Answer by xxmariofer · Jun 10, 2019 at 10:29 AM

here is a code that should work, you need to assig the target in the oinspector

 bool goingRight = true;
 public float speed = 1;

 public Vector3 endPosition;
 Vector3 startPosition;

 void Awake()
 {
     startPosition = transform.position;
 }

 private void Update()
 {
     transform.position = Vector3.Lerp(startPosition, endPosition, amount);
     
     amount = speed ^Time.deltaTime;
     if(amount >= 1 || amount <= 0) { speed = -speed; }
 }
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

Follow this Question

Answers Answers and Comments

133 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 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

Saving the starting position of a Lerp during Update 2 Answers

Mathf.Lerp not working 2 Answers

Vector3.Lerp works outside of Update() 3 Answers

mathf.pingpong 0 Answers

Mathf.PingPong from a random position problems 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