赞
踩
在unity中实现物体移动的方法有很多种,本文将介绍7种不同的实现方法,可自行参考效果
public Transform targetTansform;
In Update/FixedUpdate:
this.transform.position = targetTansform.position;
public Transform targetTansform;
public float lerpSpeed = 5f;
In Update/FixedUpdate:
this.transform.position = Vector3.Lerp(this.transform.position,targetTansform.position, lerpSpeed * Time.fixedDeltaTime);
public Transform targetTansform;
public float moveSpeed = 5f;
In Update/FixedUpdate:
this.transform.position = Vector3.MoveTowards(this.transform.position,targetTansform.position, moveSpeed * Time.fixedDeltaTime);
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);
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);
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);
public Transform targetTansform;
In Update/FixedUpdate:
Vector3 moveDirection = targetTansform.position - transform.position;
transform.Translate(moveDirection,Space.World);
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; } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。