# Unity3d 控制物体transform移动的几种方法

function Translate (translation : Vector3relativeTo : Space = Space.Self) : void

function Update() {

//导弹相对于战斗机Fighter以ShootSpeed 的速度向前运动,Vector3.forward在此时表示导弹的正前方

transform.Translate(Vector3.forward * ShootSpeed * Time.deltaTime, Fighter.transform);

}

using UnityEngine;
using System.Collections;

public class RedMove : MonoBehaviour {

public int MoveSpeed = 10;

// Update is called once per frame
void FixedUpdate () {
transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime, Space.World);
}
}

using UnityEngine;
using System.Collections;

public class BlueMove : MonoBehaviour {

public GameObject RedBall;
public int MoveSpeed = 10;

// Update is called once per frame
void FixedUpdate () {
transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime, RedBall.transform);
}
}


1、我们先让红球的坐标系各轴方向与世界坐标系的各轴方向相同，则红球与蓝球在运动过程中是相对静止的：

2、接着我们将红球绕y轴顺时针旋转90度（即使红球的Transform.Rotation.y = 90），此时红球坐标系的z轴正向和世界坐标系的x轴正向重合，此时运动效果如下：

void Start () {
gameObject.GetComponent<Rigidbody>().velocity = Vector3.forward * MoveSpeed;
}

public void MovePosition(Vector3position);

void FixedUpdate() {

//让物体向前运动Time.deltaTime距离
rb.MovePosition(transform.position + transform.forward * Time.deltaTime);
}

static function MoveTowards(current: Vector3, target: Vector3, maxDistanceDelta: float): Vector3;

using UnityEngine;
using System.Collections;
public class YellowMove : MonoBehaviour {
public int MoveSpeed = 10;
Vector3 target;

void Start () {
target = new Vector3(20, transform.position.y, 20);
}

void Update () {
transform.position = Vector3.MoveTowards(transform.position, target, MoveSpeed * Time.deltaTime);
}
}


1、使用Mathf.Lerp()函数

static functionLerp (from : float,to : float,t : float) : float

using UnityEngine;
using System.Collections;
public class YellowMove : MonoBehaviour {
public float MoveSpeed = 0.1f;
Vector3 Target = new Vector3(20, 20, 20);

//控制物体向Target移动
void Update () {
gameObject.transform.localPosition = new Vector3(
Mathf.Lerp(transform.position.x, Target.x, MoveSpeed * Time.deltaTime),
Mathf.Lerp(transform.position.y, Target.y, MoveSpeed * Time.deltaTime),
Mathf.Lerp(transform.position.z, Target.z, MoveSpeed * Time.deltaTime));
}
}

2、使用Vector3.Lerp()

public staticVector3 Lerp(Vector3a,Vector3b, floatt);

using UnityEngine;
using System.Collections;
public class YellowMove : MonoBehaviour {
public float MoveSpeed = 0.1f;
Vector3 Target = new Vector3(20, 20, 20);

//控制物体向Target移动
void Update () {
gameObject.transform.localPosition = Vector3.Lerp(transform.position, Target, MoveSpeed * Time.deltaTime),
}
}

1、使用Vector3.SmoothDamp()

static function SmoothDamp (current : Vector3,target : Vector3,ref currentVelocity : Vector3,smoothTime : float,maxSpeed : float = Mathf.Infinity,deltaTime : float = Time.deltaTime) : Vector3

using UnityEngine;
using System.Collections;

public class YellowMove : MonoBehaviour {

public float MoveSpeed = 0f;
float CurrentNum = 0f;
float TargetNum = 20f;
float MoveTime = 10f;

void Update () {

Debug.Log("当前数值：" + CurrentNum + "；当前速度：" + MoveSpeed);
CurrentNum = Mathf.SmoothDamp(CurrentNum, TargetNum, ref MoveSpeed, MoveTime * Time.deltaTime);
}

}

Vector3.SmoothDamp()用法如下：

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour{
public Transform target;
public float smoothTime = 0.3F;
private Vector3 velocity = Vector3.zero;

void Update() {

//定义一个目标位置在目标变换的上方并且在后面
Vector3 targetPosition = target.TransformPoint(new Vector3(0, 5, -10));

//平滑地移动摄像机朝向目标位置
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
}
}

2、使用Mathf.SmoothDamp()

Unity使用CharacterController（角色控制器）来控制角色骨骼运动，包括移动、跳跃以及各种动作。CharacterController比较复杂，具体详情请参照博客Unity CharacterController(角色控制器)

iTween是Unity3d的一个动画插件，可以让你更加轻松的实现各种动作，iTween实现移动的方式也比较多样，具体的可以参考博客Unity iTween动画库插件

usingUnityEngine;
Using System.Collections;

Public class RedMove: MonoBehaviour
{
public Vector3 TargetPosition;
public float MoveSpeed;

Void Start()
{
StartCoroutine(Move(TargetPosition));
}

IEnumerator Move(Vector3 target)
{
while(transform.position != target)
{
transform.position = Vector3.MoveTowards(transform.position, target, MoveSpeed * Time.deltaTime);
Yield return 0;
}
}
} 

• 本文已收录于以下专栏：

举报原因： 您举报文章：Unity3d 控制物体transform移动的几种方法 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)