码迷,mamicode.com
首页 > 移动开发 > 详细

关于用过控制人物移动的方法

时间:2017-02-23 20:36:56      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:length   screen   floor   meta   play   time   移动   int   public   

在这里将收集更新有关,控制移动的方法。

1.通过WSAD控制人物的前后移动的方法(一)

 1 public class PlayerMovemeng:monoBehaviour
 2 {
 3 //人物的移动速度
 4 public float speed=6f;
 5 //坐标的移动变化量
 6 Vector3 movement;
 7 Rigidbody playerRigidbody;
 8 //与Ray发生碰撞的平面的Layer层数’
 9 int FloorMask;
10 float camRayLength=100f;
11 void Awake()
12 {
13 floorMask=LayerMask.GetMask("Floor");
14 PlayerRigidbody=GetComponent<Rigidbody>();
15 }
16 void FixedUpdate()
17 {
18 //ws前后的控制
19 float h=Input.GetAxisRaw("Horizontal"):
20 //ad左右的控制
21 float v=Input.GetAxisRaw("Vertical")
22 Move(h,v);
23 Turning();
24 }
25 //实现移动
26 void Move(float h,float v)
27 {
28 movement.Set(h,0,v);
29 movement=movement.normalized*speed*Time.deltaTime;
30 //使用刚体的MovePosition来实现人物移动
31 playerRigidbody.MovePosition(trandorm.position+movement);
32 }
33 //转向鼠标
34 void Turning()
35 {
36 Ray camRay=Cameta.main.ScreenPointToRay(Input.mousePosition);
37 RaycastHit floorHit;
38 if(Physics.Raycast(camRay,Out floorHit,camRayLength,floorMask));
39 {
40 //获取人物与鼠标点击处的向量
41 Vevtor3 playerToMouse=floorHit.point-transform.position;
//获取人物与鼠标之间的用于旋转的四元数
42 Quaternion newRotation=Quaternion.LookRotation(playerToMouse);
//实现人物的转向过度
43 playerRigidbody.MoveRotation(newRotation); 44 } 45 } 46 }

2.实现相机跟随人物的脚本(无旋转方向)

 1 public Transform target;
 2 //镜头滑动速度
 3 public float smoothing=5f;
 4 //相机与人物的距离
 5 Vector3 offset;
 6 void Start()
 7 {
 8 offset=transform.positiion-target.position;
 9 }
10 void FixedUpdate()
11 {
12    Vevtor3 targetCamPos=target.position+offset;
13 transform.position=Vector3.Lerp(transform.position,targetCamPos,smoothing*Time.datle);
14 }

 

关于用过控制人物移动的方法

标签:length   screen   floor   meta   play   time   移动   int   public   

原文地址:http://www.cnblogs.com/HeYIcengshuo/p/6435160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!