Unity - 如何使用 Lerp 在碰撞进入和退出时为平台上下移动设置动画?

Unity - How to animate platform move up and down on collision enter and exit using Lerp?

我正在尝试为生成的平台预制件制作动画(平台对象本身在空内,因为它的位置发生变化)当 Runner 对象与其碰撞时向下 (onCollisonEnter) 并在碰撞退出时向上。

我在这里一字不差地遵循了对我的老问题的回答-Unity, C# - cannot make object move down from current y position ONCE on collision enter?但是尽管按照指示在我的动画师中使用了动画师,但仍然无法使动画工作:

一位回答者建议我使用 Lerp 完全使用代码为平台预制件制作动画。我已经研究过 Lerp,但是因为我需要 2 个独立的状态 Down/Idle Down(使平台保持向下)和 up/idle 相同,所以我不知道该怎么做。

如何以编程方式使用 Lerp 实现此效果?有没有可能达到我想要的效果?

错误:

编辑:

我如何生成(出队然后入队)我的平台:

nextPosition += new Vector3 (
            Random.Range (minGap.x, maxGap.x) + scale.x,
            Random.Range (minGap.y, maxGap.y),
            Random.Range (minGap.z, maxGap.z));


        Transform o = objectQueue.Dequeue();
        o.localScale = scale;
        o.localPosition = position;
        //o.localEulerAngles = rotation;
        o.gameObject.SetActive (true);

        int materialIndex = Random.Range(0, materials.Length);
        o.GetComponent<Renderer>().material = materials[materialIndex];
        o.GetComponent<Collider> ().material = noFrictionMaterial;

        platform newScript = new o.GetComponent<platform>(); //getting an error here when I tried to implement your code

        objectQueue.Enqueue (o);

        if(nextPosition.y < minY){
            nextPosition.y = minY + maxGap.y;
        }
        else if(nextPosition.y > maxY){
            nextPosition.y = maxY - maxGap.y;
        }

这就是向量应该始终是什么以及我最初在 start() 中设置它们的位置:

up = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
        down = new Vector3 (transform.position.x, transform.position.y-2f, transform.position.z);

由于平台实际上并没有生成,您确定这是错误吗?你能帮帮我吗?

这应该适合你:

using UnityEngine;
using System.Collections;

public class Collision : MonoBehaviour {

    public Vector3 up, down;
    public bool isUp = false;
    public float speed = 5f;

    float startTime,
        length;
    bool isMoving = false;

    void Start () {
        //initialise the platform to a position
        if (isUp) transform.position = up;
        else transform.position = down;

        length = Vector3.Distance(up, down);
    }

    void Update()
    {
        if (isUp)
        {
            //move Down
            transform.position = Vector3.Lerp(up, down, ((Time.time - startTime) * speed) / length);
        }
        else
        {
            //move Up
            transform.position = Vector3.Lerp(down, up, ((Time.time - startTime) * speed) / length);
        }
    }

    //move down
    void OnCollisionEnter(Collision col)
    {
        if (!isMoving)
        {
            startTime = Time.time;
            isMoving = true;
        }
    }

    //move up
    void OnCollisionExit(Collision col)
    {
        if (!isMoving)
        {
            startTime = Time.time;
            isMoving = true;
        }
    }
}

您需要注意的是确保 "runner" 一直停留在平台上,直到它到达底部,以便它可以再次向上移动。您可以添加一个支票,以便它向上移动,而不管另一个 bool 它对您不起作用。希望对您有所帮助:)

编辑

lerp(线性插值的缩写)所做的是根据时间分量(0 到 1)的进展在两个向量之间选取点。因此,为了让平台上下移动,它需要一个起点和一个终点,在这种情况下,它是向上或向下(向上或向下为另一个)。

如果你想创建一个新平台而不导致错误,你需要在创建它之后设置这些值,以便一切正常工作(如果你从任何位置跳跃,它会在 lerp 的第一步积极跳跃) .例如:

//after instantiating the new platform object
scriptName newObject = newGameobject.GetComponent<scriptName>();
newObject.up = (up Vector);
newObject.down = (down Vector);