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 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. This 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 this, but I'm a bit of a n00b.

The other thing I want to do, sort of related to the first thing, is have a bit of a delay whenever the platform reaches its original position again. This 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
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 Martín Coll · Apr 18, 2013 at 01:50 PM 0
Share

I've used the Sin function depending on time ins$$anonymous$$d of PingPong to have smooth transitions.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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
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
1
Wiki

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

I prefer using simple formulae to handle this 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 this helps!

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 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 this:

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

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

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

Going from 0 to 1 and then back to 0 within a time frame 1 Answer

Trying to understand directional calculations 0 Answers

Is there a way to have an object gradually move to one spot, move back, and then stop? 1 Answer

Move character to mouse position, keeping constant speed 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