U3D中几种控制物体移动的方法

一、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是刚体特有的属性。代码如下:

[csharp]  view plain  copy
  1. void Start () {  
  2.         gameObject.GetComponent<Rigidbody>().velocity = Vector3.forward * MoveSpeed;  
  3. }  
三、 使用rigbody. MovePosition ()

public void MovePosition(Vector3position);

让物体移动到新的位置position。

  1. void FixedUpdate() {  
  2.   
  3.         //让物体向前运动Time.deltaTime距离  
  4.         rb.MovePosition(transform.position + transform.forward * Time.deltaTime);  
  5. }  
四、 Vector3.MoveTowards()

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

  1. using UnityEngine;  
  2. using System.Collections;  
  3. public class YellowMove : MonoBehaviour {  
  4.     public int MoveSpeed = 10;  
  5.     Vector3 target;  
  6.   
  7.      void Start () {  
  8.         target = new Vector3(20, transform.position.y, 20);  
  9.      }  
  10.    
  11.      void Update () {  
  12.         transform.position = Vector3.MoveTowards(transform.position, target, MoveSpeed * Time.deltaTime);  
  13.      }  
  14. }  

五、使用lerp()

1、使用Mathf.Lerp()函数

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

调用该函数会返回from与to之间的插值(from + to) * t,t在0~1之间。

  1. using UnityEngine;  
  2. using System.Collections;  
  3. public class YellowMove : MonoBehaviour {  
  4.     public float MoveSpeed = 0.1f;  
  5.     Vector3 Target = new Vector3(20, 20, 20);  
  6.       
  7. <span class="space" style="white-space:pre;display:inline-block;text-indent:2em;line-height:inherit;"> </span>//控制物体向Target移动  
  8.     void Update () {  
  9.         gameObject.transform.localPosition = new Vector3(  
  10.         Mathf.Lerp(transform.position.x, Target.x, MoveSpeed * Time.deltaTime),  
  11.         Mathf.Lerp(transform.position.y, Target.y, MoveSpeed * Time.deltaTime),  
  12.         Mathf.Lerp(transform.position.z, Target.z, MoveSpeed * Time.deltaTime));  
  13.     }  
  14. }  

2、使用Vector3.Lerp()

public staticVector3 Lerp(Vector3a,Vector3b, floatt);

其使用方法与Mathf.Lerp()用法相似,不同点是Vector3.Lerp()是对三维向量进行插值,而Mathf.Lerp()是对数字进行插值。

  1. using UnityEngine;  
  2. using System.Collections;  
  3. public class YellowMove : MonoBehaviour {  
  4.     public float MoveSpeed = 0.1f;  
  5.     Vector3 Target = new Vector3(20, 20, 20);  
  6.       
  7.  //控制物体向Target移动  
  8.     void Update () {  
  9.         gameObject.transform.localPosition = Vector3.Lerp(transform.position, Target, MoveSpeed * Time.deltaTime),  
  10.     }  
  11. }  

六、

用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

九、使用协程

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值