一、Transform.translate
Transform.Translate通过设置下一步移动的矢量方向和大小进行移动。
有两种移动方式:
1.ADWS控制游戏对象上下左右移动,不涉及旋转;
2.AD通过旋转控制方向,WS控制前后移动,也可以实现受控位移。
无论哪种方式,首先都要获取键盘响应,可以通过Input.GetKey获取,也可以通过Input.GetAxisRaw获取。
方法一
float input_H = Input.GetAxisRaw("Horizontal"); //获取X方向的移动方向,如果输入A,输出-1;如果输入D,输出1。float input_V = Input.GetAxisRaw("Vertical"); //获取Z方向的移动方向,如果输入W,输出1;如果输入S,输出-1。
Vector3 v = new Vector3 (input_H, 0, input_V); //新建移动向量
v = v.normalized; //如果是斜线方向,需要对其进行标准化,统一长度为1
v = v * speed * Time.deltaTime; //乘以速度调整移动速度,乘以deltaTime防止卡顿现象
transform.Translate (v); //移动
第二种方式下的代码如下:
1
2
3
4
5
6
|
float
input_H = Input.GetAxisRaw(
"Horizontal"
);
//
float
input_V = Input.GetAxisRaw (
"Vertical"
);
//
transform.Rotate (
new
Vector3 (0, input_H, 0));
//绕y轴旋转,A键顺时针;D键逆时针
float
curSpeed = speed * input_V * Time.deltaTime;
transform.Translate (transform.forward * curSpeed,Space.World);
//沿着物体前后方向移动, 由于使用了forward,因此要指定移动的坐标系为全局坐标
|
二、制定速度移动
这种方法只能适用于刚体,因为velocity是刚体特有的属性。代码如下:
- void Start () {
- gameObject.GetComponent<Rigidbody>().velocity = Vector3.forward * MoveSpeed;
- }
public void MovePosition(Vector3position);
让物体移动到新的位置position。
- 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);
- }
- }
五、使用lerp()
1、使用Mathf.Lerp()函数
static functionLerp (from : float,to : float,t : float) : float
调用该函数会返回from与to之间的插值(from + to) * t,t在0~1之间。
- using UnityEngine;
- using System.Collections;
- public class YellowMove : MonoBehaviour {
- public float MoveSpeed = 0.1f;
- Vector3 Target = new Vector3(20, 20, 20);
- <span class="space" style="white-space:pre;display:inline-block;text-indent:2em;line-height:inherit;"> </span>//控制物体向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);
其使用方法与Mathf.Lerp()用法相似,不同点是Vector3.Lerp()是对三维向量进行插值,而Mathf.Lerp()是对数字进行插值。
- 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),
- }
- }
六、
用SmoothDamp()
1、使用Vector3.SmoothDamp()
static function SmoothDamp (current : Vector3,target : Vector3,ref currentVelocity : Vector3,smoothTime : float,maxSpeed : float = Mathf.Infinity,deltaTime : float = Time.deltaTime) : Vector3
七、使用CharacterController组件控制角色移动
八、使用iTween
九、使用协程