找回密码
 立即注册
查看: 6386|回复: 94

[基础] Unity3D 第四课 用键盘控制你的角色吧

[复制链接]
发表于 2013-3-4 22:54 | 显示全部楼层 |阅读模式
资源信息 Tutorial Information
教程名称: Unity3D 第四课 用键盘控制你的角色吧(发帖教程)
适用引擎:   (适用引擎,为空默认为Unity)
教程语种: 中文
教程等级: 1
教程格式: 图文(请用IE9以上浏览器访问本版块)
教程作者: 转载自互联网 (如有问题请短消息联系作者或发表回复)
下载地址: (兑换积分)
点击查看原图
美丽分割线
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哇……

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

评分

参与人数 2鲜花 +2 收起 理由
1287537617aa + 1
aa1287537617 + 1

查看全部评分

发表于 2013-4-8 09:02 | 显示全部楼层
请问这个和UNITY自带的控制代码有什么优点吗
发表于 2013-4-14 17:51 | 显示全部楼层
Unity3D 第四课 用键盘控制角色
发表于 2013-5-11 04:35 | 显示全部楼层

感谢楼主的无私分享!{:soso__11402694654016840197_7:}
发表于 2013-5-22 13:01 | 显示全部楼层
大哥 ,这样子是没有碰撞的啊!
发表于 2013-7-13 04:21 | 显示全部楼层

膜拜中。。。。{:soso__7524161091986203637_5:}
发表于 2017-4-9 12:06 | 显示全部楼层
楼主是超人
发表于 2017-4-9 12:28 | 显示全部楼层
好帖就是要顶
发表于 2017-4-9 12:45 | 显示全部楼层
顶顶多好
发表于 2017-4-9 12:36 | 显示全部楼层
真心顶
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-2 17:54 , Processed in 0.137095 second(s), 33 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表