当前位置:   article > 正文

Unity学习笔记 物体跟随移动的不同实现方法和效果_unity怎么让一个物体跟着另一个物体移动

unity怎么让一个物体跟着另一个物体移动

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

1. Set Position 方法

public Transform targetTansform;

In Update/FixedUpdate:
this.transform.position = targetTansform.position;
  • 1
  • 2
  • 3
  • 4

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);
  • 1
  • 2
  • 3
  • 4

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);
  • 1
  • 2
  • 3
  • 4

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);
  • 1
  • 2
  • 3
  • 4
  • 5

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);
  • 1
  • 2
  • 3
  • 4

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

7. transform.Translate() 方法

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

效果对比:

在这里插入图片描述

在这里插入图片描述

完整代码:

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;
            
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/111367
推荐阅读
相关标签
  

闽ICP备14008679号