【问题标题】:unity 2D how to move object between two positionsunity 2D 如何在两个位置之间移动对象
【发布时间】:2015-10-11 03:00:45
【问题描述】:

我有两个位置 posA 和 posB 我想在按钮单击时在这两个位置之间移动我的播放器。使用 Vetor3.Lerp 播放器只移动一次,但第二次移动不起作用。任何人都可以帮忙。

    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

【问题讨论】:

    标签: unity3d 2d lerp


    【解决方案1】:

    您搞砸了一些事情,Vector.lerp 在 Update 中使用,因为它是一个在完成之前应该对每一帧都有效的函数。
    它从“源”到“目标”工作,因此当您再次单击时,它仍会从源运行到目标,如果您想更改它,您需要以一种可以交换位置的方式设计您的代码。比如设置一个标志或创建另一个函数。 这是 Unity 开发人员关于如何使用 Lerp 的一个很好的教程。

    Vector3 Lerp Video
    Vector3 Lerp Scripting Reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 2014-08-21
      • 1970-01-01
      相关资源
      最近更新 更多