I’m currently using Lerp to create some moving obstacles to knock players to their death. The problem I have is that while the cube moves in a direction, it doesn’t move back again. Any idea what is causing this?
using UnityEngine;
using System.Collections;
public class Obstacal1 : MonoBehaviour {
public float speed = 0.00001f;
public float length = 10.0f;
//public Vector3 direction;
private bool leftOrRight;
private float startPointX;
private float startPointY;
private float startPointZ;
private Vector3 start_pos;
private Vector3 end_pos;
void Start () {
startPointX = transform.position.x;
startPointY = transform.position.y;
startPointZ = transform.position.z;
length = length + startPointZ;
start_pos = new Vector3(startPointX, startPointY, startPointZ);
end_pos = new Vector3(startPointX, startPointY, length);
leftOrRight = true;
}
// Update is called once per frame
void Update () {
Debug.Log(Time.time);
if (leftOrRight == true)
{
transform.position = Vector3.Lerp( start_pos, end_pos, Time.time);
}
if (transform.position.z == length)
{
leftOrRight = false;
}
if(leftOrRight == false)
{
transform.position = Vector3.Lerp( end_pos, start_pos, Time.time);
}
}
}