Unity学习笔记 物体跟随移动的不同实现方法和效果

在unity中实现物体移动的方法有很多种,本文将介绍7种不同的实现方法,可自行参考效果

1. Set Position 方法

public Transform targetTansform;

In Update/FixedUpdate:
this.transform.position = targetTansform.position;

2. Vector3.Lerp() 方法

public Transform targetTansform;
public float lerpSpeed = 5f;
In Update/FixedUpdate:
this.transform.position = Vector3.Lerp(this.transform.position,targetTansform.position, lerpSpeed * Time.fixedDeltaTime);

3. Vector3.MoveTowards() 方法

public Transform targetTansform;
public float moveSpeed = 5f;
In Update/FixedUpdate:
this.transform.position = Vector3.MoveTowards(this.transform.position,targetTansform.position, moveSpeed * Time.fixedDeltaTime);

4. Vector3.MoveTowards() 结合 Vector3.Lerp() 方法

public Transform targetTansform;
public float lerpSpeed = 5f;
public float moveSpeed = 5f;
In Update/FixedUpdate:
this.transform.position = Vector3.MoveTowards(this.transform.position,Vector3.Lerp(this.transform.position,targetTansform.position, lerpSpeed * Time.fixedDeltaTime), moveSpeed * Time.fixedDeltaTime);

5. Vector3.SmoothDamp() 方法

public Transform targetTansform;
public float dampSpeed = 100f;
In Update/FixedUpdate:
this.transform.position = Vector3.SmoothDamp(this.transform.position,targetTansform.position, ref velocity,dampSpeed * Time.fixedDeltaTime);

6. AddForce() 方法

public Transform targetTansform;
private Vector3 velocity = Vector3.zero;
In Update/FixedUpdate:
Vector3 force = targetTansform.position - transform.position;
force = force.normalized;
force = force * forceSpeed;
this.transform.GetComponent<Rigidbody>().AddForce(force);

7. transform.Translate() 方法

public Transform targetTansform;
In Update/FixedUpdate:
Vector3 moveDirection = targetTansform.position - transform.position;
transform.Translate(moveDirection,Space.World);

效果对比:

Unity学习笔记 物体跟随移动的不同实现方法和效果

Unity学习笔记 物体跟随移动的不同实现方法和效果

完整代码:

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

public enum FollowType
{
    SetPosition,
    Lerp,
    MoveTowards,
    MoveTowardsAndLerp,
    SmoothDamp,
    RigibodyAddForce,
    Translate
}

public class AutoFollow : MonoBehaviour
{
    public Transform targetTansform;
    public FollowType myFollowType;
    public float lerpSpeed = 5f;
    public float moveSpeed = 5f;
    public float dampSpeed = 100f;
    private Vector3 velocity = Vector3.zero;
    public float forceSpeed = 50;
  
    private void FixedUpdate()
    {

        switch (myFollowType)
        {
            case FollowType.SetPosition:
                this.transform.position = targetTansform.position;
                break;
            case FollowType.Lerp:
                this.transform.position = Vector3.Lerp(this.transform.position,targetTansform.position, lerpSpeed * Time.fixedDeltaTime);
                break;
            case FollowType.MoveTowards:
                this.transform.position = Vector3.MoveTowards(this.transform.position,targetTansform.position, moveSpeed * Time.fixedDeltaTime);
                break;
            case FollowType.MoveTowardsAndLerp:
                this.transform.position = Vector3.MoveTowards(this.transform.position,Vector3.Lerp(this.transform.position,targetTansform.position, lerpSpeed * Time.fixedDeltaTime), moveSpeed * Time.fixedDeltaTime);
                break;
            case FollowType.SmoothDamp:
                this.transform.position = Vector3.SmoothDamp(this.transform.position,targetTansform.position, ref velocity,dampSpeed * Time.fixedDeltaTime);
                break;
            case FollowType.RigibodyAddForce:
                Vector3 force = targetTansform.position - transform.position;
                force = force.normalized;
                force = force * forceSpeed;
                transform.GetComponent<Rigidbody>().AddForce(force);
                break;
            case FollowType.Translate:
                Vector3 moveDirection = targetTansform.position - transform.position;
                transform.Translate(moveDirection,Space.World);
                break;
            
        }
    }
}

上一篇:Unity 如何延时运行或者重复运行函数?


下一篇:111. 二叉树的最小深度