Comments and answers for "Moving one GameObject to another, and Vector Normalization issues"
http://answers.unity.com/questions/1163829/moving-one-gameobject-to-another-and-vector-normal.html
The latest comments and answers for the question "Moving one GameObject to another, and Vector Normalization issues"Answer by Dave-Carlile
http://answers.unity.com/answers/1164182/view.html
One thing you might try is to clamp the position to the target position. Online 27 and 37 where you check if it has reached the target...
if (transform.position.y >= end_transform.y)
{
... your stuff to change direction...
var position = transform.position;
transform.position = new Vector3(p.x, end_transform.y, p.z); // set y to the target y
}
Not sure if that will make your other math work out properly or not.
Another way of moving a platform back and forth is to use a linear interpolation function (Lerp) such as [Vector3.Lerp][1]. It takes as input the end points of your line, and a value *t* that varies between 0 an d 1. When you pass in 0 for *t* it will return the first point. When you pass in 1 for *t* it will return the second point. And when you pass in a value in between 0 and 1 it will return a point between the first and second end points. I like to think of *t* as a percentage - e.g. 0.5 will return the point 50% between the two input points. 0.25 would return the point 25% of the way between the input points.
So your platform position becomes...
transform.position = Vector3.Lerp(start_point, end_point, t);
Now all you have to do is figure out how to change *t* over time. One way to do that is very similar to what you're already doing with your vectors. Add Time.deltaTime * direction to *t* each frame where *direction* is +1 or -1. When *t* > 1 set direction to -1, when t < 0 set direction to +1.
Another option that is a bit more straightforward, but a bit more blackbox, is to use [Mathf.PingPong][2]. This function takes any value that changes over time and "ping pongs" it between 0 and a value of your choosing. So...
float t = Mathf.PingPong(Time.time, 1);
transform.position = Vector3.Lerp(start_point, end_point, t);
This will move *t* between 0 and 1, then back toward 0, then back toward 1, and so on. Plugging that into your *Lerp* function will automatically move the transform back and forth between your two points.
Now you can do some interesting things with *t* using Easing Functions. Mathf has one called [SmoothStep][3]. This will cause *t* to change a little more slowly as it approaches 0 or 1, so your platform will ease into those positions instead of an instant stop and change direction. You can Google Easing Functions and find a lot more interesting things with those.
[1]: http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html
[2]: http://docs.unity3d.com/ScriptReference/Mathf.PingPong.html
[3]: http://docs.unity3d.com/ScriptReference/Mathf.SmoothStep.htmlFri, 01 Apr 2016 19:01:25 GMTDave-Carlile