unity 2D 如何在两个位置之间移动对象

unity 2D how to move object between two positions

我有两个位置 posA 和 posB 我想在按钮上的这两个位置之间移动我的播放器 click.using Vetor3.Lerp 播放器只移动一次但它没有第二次工作 move.can 任何一个帮助。

    using UnityEngine;
using System.Collections;

public class PlayerTurn : MonoBehaviour {

    public Transform leftPos;
    public Transform rightPos;

    public float speed = 5;

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {
    }

    public void functionForButton(){
            transform.position = Vector3.Lerp (leftPos.position, rightPos.position, speed);
    }
}

screen

你搞砸了一些事情,Vector.lerp 用于更新,因为它是一个应该在每一帧都起作用的函数,直到它完成。
它从'Source'到'Destination'有效,所以当你再次点击时,它仍然会从源到目标运行,如果你想改变它,你需要设计你的代码一种交换位置的方式。比如设置标志或创建另一个功能。 这是 Unity 开发人员关于如何使用 Lerp 的很好的教程。

Vector3 Lerp Video
Vector3 Lerp Scripting Reference