• 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
Question by TaylorAnderson · Aug 09, 2012 at 03:03 AM · movementmathfplatformsvector3.lerppingpong

Vector3.lerp and Mathf.pingpong not moving smoothly

I have platforms that I want to move back and forth from their original position to a set position. However, I want these to be controlled by a switch, and I need individual platforms to start moving later than others. T$$anonymous$$s code works, but the platforms tend to jump suddenly from their original position closer to the new one, before moving smoothly. There's probably an easy way to fix t$$anonymous$$s, but I'm a bit of a n00b.

The other t$$anonymous$$ng I want to do, sort of related to the first t$$anonymous$$ng, is have a bit of a delay whenever the platform reaches its original position again. T$$anonymous$$s is so that there's more skill involved when trying to time each jump onto the moving platform.

Any help would be appreciated.

 using UnityEngine;
 using System.Collections;
 
 public class AscendPlatformScript : MonoBehaviour {
 public AscendSwitchScript ascendSwitch;
 private Vector3 originalPos;
 public Vector3 desiredPos;
 public float speed;
 public float timeDelay;
  void Start ()
  {
  originalPos=transform.position;
  }
  
  void Update () 
  {
  timeDelay-=Time.deltaTime;
  if (ascendSwitch.isSwitched==true && timeDelay<0)
  {
  transform.position=Vector3.Lerp(originalPos, desiredPos, Mathf.PingPong(Time.time*speed, 1.0f));
  }
  }
 }
Comment

People who like this

0 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 Martín Coll · Apr 18, 2013 at 01:50 PM 0
Share

I've used the Sin function depending on time instead of PingPong to have smooth transitions.

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by robertbu · Apr 18, 2013 at 02:08 PM

The jump is because you are using Time.time in the PingPong. Because Time.time can have an arbitrary value, you don't know where it is in the cycle. Instead use your own timer. At the top of the file:

 private timer = 0.0f;

In Update:

 timer += Time.deltaTime;
 transform.position=Vector3.Lerp(originalPos, desiredPos, Mathf.PingPong(timer*speed, 1.0f));

As for the delay, the platform will pause if the timer is not incremented.

Comment
W4rf4c3

People who like this

1 Show 0 · 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

Answer by alexanderbrevig · Apr 18, 2013 at 04:31 PM

The problem here is that you do not know the value of Time.time at the time your if is true.

You should note the time when the if becomes true and use that as an offset on the argument to Mathf.PingPong like t$$anonymous$$s:

 using UnityEngine;
 using System.Collections;
 
 public class AscendPlatformScript : MonoBehaviour {
     public AscendSwitchScript ascendSwitch;
     public Vector3 desiredPos;
     public float speed;
     public float timeDelay;
     void Start ()
     {
         originalPos=transform.position;
     }
 
     void Update () 
     {
         timeDelay-=Time.deltaTime;
         if (ascendSwitch.isSwitched==true && timeDelay<0)
         {
             startLerpAt = Time.time;
         } else {
             startLerpAt = 0;
         }
         if (startLerpAt > 0){
             transform.position=Vector3.Lerp(originalPos, desiredPos, Mathf.PingPong((Time.time-startLerpAt)*speed, 1.0f));
         }
     }
     private Vector3 originalPos;
     private float startLerpAt = 0;
 }
Comment

People who like this

0 Show 0 · 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
Wiki

Answer by Martín Coll · Apr 19, 2013 at 04:38 PM

I prefer using simple formulae to handle t$$anonymous$$s stuff, as ifs add too much processing. As I mentioned, you could use the Mathf.Sin function:

 using UnityEngine;
 using System.Collections;
 
 public class SinusoidalPlatform : MonoBehaviour {
     public Vector3 desiredPos;
     
     private Vector3 center, direction;
     
     void Start () {
         var originalPos = transform.position;
         center = originalPos + ((desiredPos - originalPos) / 2f);
         direction = (desiredPos - originalPos).normalized;
     }
     
     void Update () {
         transform.position = center + Mathf.Sin(Time.time) * direction;
     }
 }

Hope t$$anonymous$$s helps!

Comment
zhool

People who like this

1 Show 0 · 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.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Platforms Oscillating with Mathf.PingPong not working properly 0 Answers

moving player with platform csharp 1 Answer

Mathf function on GetKey 1 Answer

C# Mathf.PingPong Rotate Back and Forth 2 Answers

Unable to move Gameobject properly and smoothly to where mouse clicked , using Vector3.Lerp 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