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哇……
请问这个和UNITY自带的控制代码有什么优点吗
Unity3D 第四课 用键盘控制角色
感谢楼主的无私分享!{:soso__11402694654016840197_7:} 大哥 ,这样子是没有碰撞的啊!
膜拜中。。。。{:soso__7524161091986203637_5:} 楼主是超人 好帖就是要顶 顶顶多好 真心顶