Unity3D摇杆


本章博文的目的是利用上一章介绍的游戏摇杆来控制人物模型的移动(见  ),与行走动画的播放。

如上图所示Create中的文件夹male中存放着模型动画与贴图等,这个应该是美术提供给我们的。然后将整个male用鼠标拖动到左侧3D世界中,通过移动,旋转,缩放将人物模型放置在一个理想的位置。右侧红框内设置模型动画的属性。

Animation

idle1  该模型默认动画名称为idle1

Animations

size   该模型动画的数量

Element 该模型的动画名称

Play Automatically 是否自动播放

Animation Physics 是否设置该模型物理碰撞

Animation Only if Visable 是否设置该模型仅自己显示

给该模型绑定一个脚本Controller.cs 用来接收摇杆返回的信息更新模型动画。

Controller.cs

  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4.   
  5.   
  6. public class Controller : MonoBehaviour {  
  7.       
  8.     //人物的行走方向状态  
  9.     public const int HERO_UP0;  
  10.     public const int HERO_RIGHT1;  
  11.     public const int HERO_DOWN2;  
  12.     public const int HERO_LEFT3;  
  13.       
  14.     //人物当前行走方向状态  
  15.     public int state = 0;  
  16.       
  17.     //备份上一次人物当前行走方向状态  
  18.     //这里暂时没有用到  
  19.     public int backState = 0;  
  20.       
  21.     //游戏摇杆对象  
  22.     public MPJoystick moveJoystick;    
  23.       
  24.     //这个方法只调用一次,在Start方法之前调用  
  25.     public void Awake() {  
  26.           
  27.     }  
  28.       
  29.     //这个方法只调用一次,在Awake方法之后调用  
  30.     void Start () {  
  31.         state = HERO_DOWN;  
  32.     }  
  33.       
  34.       
  35.     void Update () {  
  36.       
  37.     //获取摇杆控制的方向数据 上一章有详细介绍    
  38.     float touchKey_x =  moveJoystick.position.x;    
  39.     float touchKey_y =  moveJoystick.position.y;    
  40.           
  41.         
  42.        
  43.     if(touchKey_x == -1){    
  44.        setHeroState(HERO_LEFT);  
  45.             
  46.     }else if(touchKey_x == 1){    
  47.        setHeroState(HERO_RIGHT);  
  48.             
  49.     }    
  50.        
  51.     if(touchKey_y == -1){    
  52.         setHeroState(HERO_DOWN);  
  53.    
  54.     }else if(touchKey_y == 1){    
  55.         setHeroState(HERO_UP);           
  56.     }    
  57.       
  58.     if(touchKey_x == 0 && touchKey_y ==0){  
  59.         //松开摇杆后播放默认动画,  
  60.         //不穿参数为播放默认动画。  
  61.         animation.Play();  
  62.     }  
  63.       
  64.           
  65.     }  
  66.       
  67.     public void setHeroState(int newState)  
  68.     {  
  69.           
  70.         //根据当前人物方向 与上一次备份方向计算出模型旋转的角度  
  71.         int rotateValue = (newState - state) * 90;  
  72.         Vector3 transformValue = new Vector3();  
  73.           
  74.         //播放行走动画  
  75.         animation.Play("walk");  
  76.           
  77.         //模型移动的位移的数值  
  78.         switch(newState){  
  79.             case HERO_UP:  
  80.                 transformValue = Vector3.forward * Time.deltaTime;  
  81.             break;    
  82.             case HERO_DOWN:  
  83.                 transformValue = -Vector3.forward * Time.deltaTime;  
  84.             break;    
  85.             case HERO_LEFT:  
  86.                 transformValue = Vector3.left * Time.deltaTime;  
  87.                   
  88.             break;    
  89.             case HERO_RIGHT:  
  90.                 transformValue = -Vector3.left * Time.deltaTime;  
  91.             break;                
  92.         }  
  93.           
  94.           
  95.         //模型旋转  
  96.         transform.Rotate(Vector3.up, rotateValue);  
  97.           
  98.         //模型移动  
  99.         transform.Translate(transformValue, Space.World);  
  100.           
  101.         backState = state;  
  102.         state = newState;  
  103.           
  104.     }  
  105.       
  106. }  

相关内容