fjkrl 发表于 2013-3-4 22:54

Unity3D 第四课 用键盘控制你的角色吧

Unity3D 第四课 亲 …用键盘控制你的角色吧

这次我们要用到之前的MouseLook.cs
然后在加入一个角色控制类,我们把这个类命名为PlayController,角色控制类,
首先我们要定意人物的4个方向,如下代码
[*]      //人物行走的4个方向
[*]      public const int PLAY_UP = 0;
[*]      public const int PLAY_RIGHT = 1;
[*]      public const int PLAY_DOWN = 2;
[*]      public const int PLAY_LEFT = 3;

复制代码
初始化速度,当前状态,
[*]//人物移动的速度
[*]      private int moveSpeed = 10;
[*]      //记录当前人物的状态
[*]      private int gameState = 0;
[*]

复制代码
获取按钮,这里我们用W,S,A,D,大众化游戏的操控undefined
[*]//获取按钮A
[*]Input.GetKey(KeyCode.A)

复制代码
以下各个按钮直接KeyCode.*;      注:*代替各个按钮
[*]//初始化当前状态
[*]void Awake ()
[*]{
[*]      gameState = PLAY_DOWN;
[*]}

复制代码
移动方法
[*]         public void setGameState(int newState)
[*]      {
[*]                //根据当前模型方向一上一次备份的方向计算出模型的角度
[*]                int rotareValue = (newState - gameState) * 90;
[*]                Vector3 transFormValue = new Vector3();
[*]                //模型移动的数值
[*]                switch(newState)
[*]                {
[*]                        case PLAY_UP:
[*]                              transFormValue = Vector3.forward * Time.deltaTime;
[*]                              break;
[*]                        case PLAY_DOWN:
[*]                              transFormValue = (-Vector3.forward) * Time.deltaTime;
[*]                              break;
[*]                        case PLAY_LEFT:
[*]                              transFormValue = Vector3.left * Time.deltaTime;
[*]                              break;
[*]                        case PLAY_RIGHT:
[*]                              transFormValue = (-Vector3.left) * Time.deltaTime;
[*]                              break;
[*]                }
[*]                //模型旋转
[*]                transform.Rotate(Vector3.up ,rotareValue);
[*]                //移动人物
[*]                transform.Translate(transFormValue * moveSpeed,Space.World);
[*]                gameState = newState;
[*]
[*]      }

复制代码
好了以下帖出全部代码:
[*]using UnityEngine;
[*]using System.Collections;
[*]
[*]public class PlayController: MonoBehaviour
[*]{
[*]
[*]      //人物行走的4个方向
[*]      public const int PLAY_UP = 0;
[*]      public const int PLAY_RIGHT = 1;
[*]      public const int PLAY_DOWN = 2;
[*]      public const int PLAY_LEFT = 3;
[*]
[*]      //记录当前人物的状态
[*]      private int gameState = 0;
[*]
[*]      //人物移动的速度
[*]      private int moveSpeed = 10;
[*]      void Awake ()
[*]      {
[*]                gameState = PLAY_DOWN;
[*]      }
[*]
[*]      void Update ()
[*]      {
[*]                KeyMover();
[*]      }
[*]
[*]      void KeyMover()
[*]      {
[*]                if(Input.GetKey(KeyCode.A))
[*]                {
[*]                        setGameState(PLAY_LEFT);
[*]                }
[*]                else if(Input.GetKey(KeyCode.D))
[*]                {
[*]                        setGameState(PLAY_RIGHT);
[*]                }
[*]                else if(Input.GetKey(KeyCode.S))
[*]                {
[*]                        setGameState(PLAY_DOWN);
[*]                }
[*]                else if(Input.GetKey(KeyCode.W))
[*]                {
[*]                        setGameState(PLAY_UP);
[*]                }
[*]      }
[*]
[*]         public void setGameState(int newState)
[*]      {
[*]                //根据当前模型方向一上一次备份的方向计算出模型的角度
[*]                int rotareValue = (newState - gameState) * 90;
[*]                Vector3 transFormValue = new Vector3();
[*]                //模型移动的数值
[*]                switch(newState)
[*]                {
[*]                        case PLAY_UP:
[*]                              transFormValue = Vector3.forward * Time.deltaTime;
[*]                              break;
[*]                        case PLAY_DOWN:
[*]                              transFormValue = (-Vector3.forward) * Time.deltaTime;
[*]                              break;
[*]                        case PLAY_LEFT:
[*]                              transFormValue = Vector3.left * Time.deltaTime;
[*]                              break;
[*]                        case PLAY_RIGHT:
[*]                              transFormValue = (-Vector3.left) * Time.deltaTime;
[*]                              break;
[*]                }
[*]                //模型旋转
[*]                transform.Rotate(Vector3.up ,rotareValue);
[*]                //移动人物
[*]                transform.Translate(transFormValue * moveSpeed,Space.World);
[*]                gameState = newState;
[*]
[*]      }
[*]}

复制代码
小技巧:按键的获取方法2
[*]                float KeyVertical = Input.GetAxis("Vertical");
[*]                float KeyHorizontal = Input.GetAxis("Horizontal");
[*]                if(KeyVertical == -1)
[*]                {
[*]                        setGameState(PLAY_LEFT);
[*]                }
[*]                else if(KeyVertical == 1)
[*]                {
[*]                        setGameState(PLAY_RIGHT);
[*]                }
[*]
[*]                if(KeyHorizontal == 1)
[*]                {
[*]                        setGameState(PLAY_DOWN);
[*]                }
[*]                else if(KeyHorizontal == 1)
[*]                {
[*]                        setGameState(PLAY_UP);
[*]                }

复制代码
这样也是能获取按钮的哦…
大家结合之前的MouseLook,试试,是不是很NX哇……


474831404 发表于 2013-4-8 09:02

请问这个和UNITY自带的控制代码有什么优点吗

跨光速 发表于 2013-4-14 17:51

Unity3D 第四课 用键盘控制角色

紅日逆刃 发表于 2013-5-11 04:35


感谢楼主的无私分享!{:soso__11402694654016840197_7:}

tkm614 发表于 2013-5-22 13:01

大哥 ,这样子是没有碰撞的啊!

失落的羽翼 发表于 2013-7-13 04:21


膜拜中。。。。{:soso__7524161091986203637_5:}

dream4java 发表于 2017-4-9 12:06

楼主是超人

yezhinet 发表于 2017-4-9 12:28

好帖就是要顶

hufuyu 发表于 2017-4-9 12:45

顶顶多好

zhangyshun 发表于 2017-4-9 12:36

真心顶
页: [1] 2 3 4 5 6
查看完整版本: Unity3D 第四课 用键盘控制你的角色吧