Unity控制移动的几种方法

补充:

指定速度velocity

这种方法只能适用于刚体,因为velocity是刚体特有的属性。代码如下:

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


1.rigidbody.MovePosition()控制物体上下左右移动(简单好用)

[csharp]  view plain  copy
  1. // Update is called once per frame  
  2.     void Update()  
  3.     {  
  4.         //控制移动  
  5.         float h = Input.GetAxis("Horizontal");  
  6.         float v = Input.GetAxis("Vertical");  
  7.   
  8.         rigidbody.MovePosition(transform.position + new Vector3(h, 0, v) * speed * Time.deltaTime);  
  9.     }  

2.transform.Translate();上下左右移动

[csharp]  view plain  copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class Move : MonoBehaviour  
  5. {  
  6.     public float speed = 5;  
  7.     private Transform transform;  
  8.   
  9.     // Use this for initialization  
  10.     void Start()  
  11.     {  
  12.         transform = this.GetComponent<Transform>();  
  13.     }  
  14.   
  15.     // Update is called once per frame  
  16.     void Update()  
  17.     {  
  18.         float h = Input.GetAxis("Horizontal");  
  19.         float v = Input.GetAxis("Vertical");  
  20.   
  21.         transform.Translate(new Vector3(h, 0, v) * speed * Time.deltaTime);  
  22.     }  
  23. }  

3.点击鼠标,物体移动到鼠标位置(2D,transform.position)

[csharp]  view plain  copy
  1. if (isMouseDown && GameManager._instance.gameState == GameState.Runing)  
  2.         {  
  3.             if (lastMousePosition != Vector3.zero)  
  4.             {  
  5.                 //Camera.main.ScreenToWorldPoint(Input.mousePosition)  
  6.                 Vector3 offest = Camera.main.ScreenToWorldPoint(Input.mousePosition) - lastMousePosition;//位移  
  7.                 transform.position = transform.position + offest;  
  8.                 checkPosition();  
  9.             }  
  10.             lastMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);  
  11.         }  

4.rigidbody2D.AddForce();(2D游戏添加力左右移动物体,忍者跑酷)

[csharp]  view plain  copy
  1. void Update()  
  2.     {  
  3.         if (isSlide == false)  
  4.         {  
  5.             float h = Input.GetAxis("Horizontal");  
  6.             Vector2 velocity = rigidbody2D.velocity;  
  7.   
  8.   
  9.             if (h > 0.05f)  
  10.             {  
  11.                 rigidbody2D.AddForce(Vector2.right * force_move);  
  12.             }  
  13.             else if (h < -0.05f)  
  14.             {  
  15.                 rigidbody2D.AddForce(-Vector2.right * force_move);  
  16.             }  
  17.     }  
 

5.点击鼠标,物体移动到鼠标的位置(3D,Vector3.MoveToWards()函数,BeatPlane飞机大战)

实现代码:

[csharp]  view plain  copy
  1. protected Transform _transform;  
  2. protected Vector3 targetPos;//目标位置  
  3.   
  4.     // Use this for initialization  
  5.     void Start()  
  6.     {  
  7.         _transform = this.transform;  
  8.         targetPos = this._transform.position;  
  9.     }  
  10.   
  11.     void MoveTo()  
  12.     {  
  13.         if (Input.GetMouseButton(0))  
  14.         {  
  15.             //获得鼠标屏幕位置  
  16.             Vector3 mousePos = Input.mousePosition;  
  17.             //将屏幕位置转为射线  
  18.             Ray ray = Camera.main.ScreenPointToRay(mousePos);  
  19.             //用来记录射线碰撞记录  
  20.             RaycastHit hitInfo;  
  21.             //产生射线  
  22.             bool isCast = Physics.Raycast(ray, out hitInfo, 1000, inputMask);  
  23.             if (isCast)  
  24.             {  
  25.                 //如果射中目标,记录射线碰撞点   
  26.                 targetPos = hitInfo.point;  
  27.             }  
  28.         }  
  29.         //使用Vector3提供的MoveTowards函数,获得朝目标移动的位置  
  30.         Vector3 pos = Vector3.MoveTowards(this._transform.position, targetPos, speed * Time.deltaTime);  
  31.         //更新当前位置  
  32.         this._transform.position = pos;  
  33.     }  

游戏运行时,点击鼠标,物体就会移动到鼠标所点击的位置。

6.键盘WSAD控制物体的上下左右移动(这个比较复杂。。)

[csharp]  view plain  copy
  1. void Control()  
  2.     {  
  3.   
  4.         //获取鼠标移动距离  
  5.         float rh = Input.GetAxis("Mouse X");  
  6.         float rv = Input.GetAxis("Mouse Y");  
  7.   
  8.         // 旋转摄像机  
  9.         m_camRot.x -= rv;  
  10.         m_camRot.y += rh;  
  11.         m_camTransform.eulerAngles = m_camRot;  
  12.   
  13.         // 使主角的面向方向与摄像机一致  
  14.         Vector3 camrot = m_camTransform.eulerAngles;  
  15.         camrot.x = 0; camrot.z = 0;  
  16.         m_transform.eulerAngles = camrot;  
  17.   
  18.         // 定义3个值控制移动  
  19.         float xm = 0, ym = 0, zm = 0;  
  20.   
  21.         // 重力运动  
  22.         ym -= m_gravity * Time.deltaTime;  
  23.   
  24.         //按键盘W向上移动  
  25.         if (Input.GetKey(KeyCode.W))  
  26.         {  
  27.             zm += m_movSpeed * Time.deltaTime;  
  28.         }  
  29.         else if (Input.GetKey(KeyCode.S))//按键盘S向下移动  
  30.         {  
  31.             zm -= m_movSpeed * Time.deltaTime;  
  32.         }  
  33.   
  34.         if (Input.GetKey(KeyCode.A))//按键盘A向左移动  
  35.         {  
  36.             xm -= m_movSpeed * Time.deltaTime;  
  37.         }  
  38.         else if (Input.GetKey(KeyCode.D))//按键盘D向右移动  
  39.         {  
  40.             xm += m_movSpeed * Time.deltaTime;  
  41.         }<pre name="code" class="csharp">}   
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值