Moving Platform Script Doesn't Work

I made this straightforward script:

var target : GameObject;
var origin : GameObject;
var speed : float;

function Update () {
   
    step = speed * Time.deltaTime;

    if(transform.position == target.transform.position){
        transform.position = Vector3.MoveTowards(transform.position, origin.transform.position, step);
    }
   
    if(transform.position == origin.transform.position){
        transform.position = Vector3.MoveTowards(transform.position, target.transform.position, step);
    }
}

But the problem is instead of the object moving back and forth it moves towards the target and before it reaches the target it stops moving and spazzes out. I don’t get any errors either. Please help!! :frowning:

Check the asset store for itween.
Ya you could fix that script but just check out that asset because it free and great for moving items.

Your problem with that script above is you need more of a area or distance calculation to get it right. Could solve with 2 simple collider trigger as well I am sure.

Well, your script is made so that if the current position is not equal to either the origin or target, it will not move.

The best way to fix this is a state-based system instead, that can even be simulated by a simple boolean.

If this is not enough to get you on the way to a solution, say it, I’ll help with something a bit more precise.