一月末黑的博客

不会程序的美术不是好策划~一个美术学代码用来做笔记的地方

导航

Unity全视角游戏的键盘操作位移——研究笔记

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class MoveCeShi : MonoBehaviour 
 5 {
 6     public float m_Speed = 5;
 7 
 8     private CharacterController m_cc;
 9 
10     void Start () 
11     {
12         m_cc = this.GetComponent<CharacterController>();
13     }
14 
15     void Update () 
16     {
17         float h = Input.GetAxis("Horizontal");
18         float v = Input.GetAxis("Vertical");
19         if (Mathf.Abs(h) > 0.05f || Mathf.Abs(v) > 0.05f) 
20         {
21             var dir = new Vector3(h, v, 0);
22             Rotate(dir);
23             Move();
24         }
25 
26     }
27     void Move()
28     {
29 
30         m_cc.SimpleMove(this.transform.forward * m_Speed);
31     }
32 
33     void Rotate(Vector3 Dir)
34     {
35         Vector3 ScreenPos = Camera.main.WorldToScreenPoint(this.transform.position);
36         Vector3 DestPoint = ScreenPos + Dir*2;
37         Vector3 WorldPos =  Camera.main.ScreenToWorldPoint(DestPoint);
38         var tagetPos = new Vector3(WorldPos.x, this.transform.position.y, WorldPos.z);
39         this.transform.LookAt(tagetPos);
40 
41     }
42 }

posted on 2016-12-05 20:46  一月末黑  阅读(468)  评论(0编辑  收藏  举报