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));
}
}
}
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.
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!
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;
}