Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

c# - How can I use Mathf.Lerp to change smooth position of an object?

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;

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.

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.

Original and Destination

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...