Moving object continuously using translate

时间:2016-04-12 00:46:12

标签: unity3d

I want to move an object from A to B continuously,like 1st A to B then B to A then again A to B and so on,thanks in advance. I've tried this.

float speed X = 1; float speed Y = 0; float speed Z = 0;
 // Use this for initialization
 void Start () {

 }

 // Update is called once per frame
void Update () {
     transform.Translate (new Vector 3 (speed X, speed Y, speed Z) * Time . delta time ); 
 }

1 个答案:

答案 0 :(得分:0)

您应该使用Vector3.Lerp:

  

在两个向量之间进行线性插值。

     

通过插值t在矢量a和b之间插值。该   参数t被钳位到[0,1]范围内。这是最常用的   沿着两条线之间的线找到一个点的一部分   端点(例如,在这些点之间逐渐移动对象。(read this

虽然您的实际答案是代码,但您可以看到:

  1. http://answers.unity3d.com/questions/14279/make-an-object-move-from-point-a-to-point-b-then-b.html
  2. http://answers.unity3d.com/questions/905966/moving-an-object-constantly-between-two-points-wit.html
  3. How to move an object between two points in Unity?
  4. http://answers.unity3d.com/questions/690884/how-to-move-an-object-along-x-axis-between-two-poi.html