Lerp Probleam

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

In your if statement you are tring to see if a float is equal to something. It almost never will be. Try equal or greater than instead. (>=) Or equal or less than. (<=).

True, but I’m not sure that the problem. I’ve debugged the if statement and the error isn’t there. I think the problem is with the second lerp. I think something in the End Vector must be position the cube back at the start, instead of moving it there.

I maybe off here, but is your problem just that it position your cube to end point instead of moving it? If so you should look last parameter of lerp() function. It takes value from 0 to 1. And giving Time.time as parameter would always result of positioning cube to end position instead of moving it there over random time.