如何使用 Mathf.Lerp 更改对象的平滑位置? [英] How can I use Mathf.Lerp to change smooth position of an object?

查看:59
本文介绍了如何使用 Mathf.Lerp 更改对象的平滑位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveNavi : MonoBehaviour
{
    public Transform destinationTransform
    public float speed;
    public float distanceToStop;
    public float lerpTime;
    public static bool naviChildOfHand = false;

    private GameObject rig_f_middle;
    private bool changeNaviChild = false;

    // Start is called before the first frame update
    void Start()
    {
        rig_f_middle = GameObject.Find("rig_f_middle.02.R");
    }

    // Update is called once per frame
    void Update()
    {
        if (IKControl.startMovingNAVI == true)
        {
            var v = rig_f_middle.transform.position - transform.position;
            if (v.magnitude < 0.001f)
            {
                //this.transform.parent = rig_f_middle.transform;
                //this.enabled = false;
                naviChildOfHand = true;
                changeNaviChild = true;
                return;
            }
            Vector3 moveDir = v.normalized;
            transform.position += moveDir * speed * Time.deltaTime;
        }

        if(changeNaviChild == true)
        {
            this.transform.position = Mathf.Lerp(this.transform.position,
                destinationTransform.position, lerpTime * Time.deltaTime);
        }
    }
}

相反,只需使用此行将对象子项更改为另一个父项:

Instead just changing the object child to another parent with this line :

this.transform.parent = rig_f_middle.transform;

我在两个地方复制了对象,当游戏开始时,这个变换是父对象的子对象,destinationTransform 是变换对象的另一个副本,我想使用 Mathf Lerp 来平滑地从变换切换到 destinationTransform.

I have duplicated the object in two places this transform when the game start it's child of a parent and destinationTransform is another copy of the transform object and I want to use the Mathf Lerp to smooth switch from the transform to the destinationTransform.

这是截图:我用红色圆圈标记了目的地名称 NAVI Destination 它与 NAVI Original 的对象相同.而且我想使用 Lerp 来平滑原始到目的地之间的变化,这样原始将慢慢变为禁用,而目的地将以某种方式启用.

Here is a screenshot : I marked with red circle the destination name NAVI Destination it's the same object as the NAVI Original. And I want to use Lerp to smooth change between the original to the destination so the slowly the original will become disable and the destination will become enabled somehow.

主要目标是平滑过渡,将 NAVI 作为子代从一个父代更改为另一个父代.

The main goal is to make a smooth transition changing the NAVI as child from one parent to another parent.

推荐答案

您需要做的是引用原始变换和目标变换,然后是这两个位置之间的 Lerp.到达目标后,将此转换作为目标转换的父级.

What you'll want to do is have references to the origin transform, and the destination transform, then Lerp between those two positions. Once you've reached the destination, then parent this transform to the destination transform.

您可以使用 Vector3.Lerp 在两个向量之间进行 lerp.

You can use the Vector3.Lerp to lerp between two vectors.

像这样(已编写但未经测试):

Like this (written but untested):

public class MoveNavi : MonoBehaviour
{
    public enum TransitionState
    {
        None,
        MovingTowards,
        Transferring
    }

    public Transform destinationTransform;
    public float speed;
    public float distanceToStop;
    public float lerpTime;
    public static bool naviChildOfHand = false;

    private GameObject rig_f_middle;
    private bool changeNaviChild = false;

    private Transform originTransform;
    private float timer;

    private TransitionState state = TransitionState.MovingTowards;

    void Start ( )
    {
        rig_f_middle = GameObject.Find ( "rig_f_middle.02.R" );
    }

    void Update ( )
    {
        switch ( state )
        {
            case TransitionState.MovingTowards:
                var v = rig_f_middle.transform.position - transform.position;
                if ( v.magnitude < 0.001f )
                {
                    state = TransitionState.Transferring;
                    originTransform = rig_f_middle.transform;
                    timer = 0;
                    return;
                }
                Vector3 moveDir = v.normalized;
                transform.position += moveDir * speed * Time.deltaTime;
            break;

            case TransitionState.Transferring:
                timer += Time.deltaTime;
                this.transform.position = Vector3.Lerp ( originTransform.position, destinationTransform.position, timer );
                if ( timer >= 1.0f )
                {
                    this.transform.parent = destinationTransform;
                    state = TransitionState.None;
                    this.enabled = false;
                    return;
                }
            break;

            default:
                this.enabled = false;
                return;
        }
    }
}

如果不是全部,那应该可以帮助您找到解决方案.

That should get you most, if not all, the way towards a solution.

这篇关于如何使用 Mathf.Lerp 更改对象的平滑位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆