码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-12-05 23:13:10      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:oat   private   code   etc   cte   rac   cto   sys   键盘   

 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 }

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

标签:oat   private   code   etc   cte   rac   cto   sys   键盘   

原文地址:http://www.cnblogs.com/heizai/p/6135429.html

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