zoukankan      html  css  js  c++  java
  • unity3d之控制人物转向移动并播放动画

    tip:transition 勾选Has Exit Time B动画播放完毕后就可以自己返回A不用代码控制。因为想做一个小人静止时 隔两秒会摆动小手的特效。

     附上代码参考:

      1 using UnityEngine;
      2 using System.Collections;
      3 
      4 public class playeMove : MonoBehaviour
      5 {
      6 
      7     public Animator PlayerAnimator;
      8     public const int HERO_UP = 0;
      9     public const int HERO_RIGHT = 1;
     10     public const int HERO_DOWN = 2;
     11     public const int HERO_LEFT = 3;
     12     float  FreakTime=3;
     13     //人物当前行走的方向状态  
     14     public int state = 0;
     15     //人物移动速度  
     16     public int moveSpeed =2;
     17 
     18     //初始化人物位置  
     19     public void Awake()
     20     {
     21         state = HERO_UP;
     22     }
     23     // Use this for initialization  
     24     void Start()
     25     {
     26 
     27     }
     28 
     29     // Update is called once per frame  
     30     void Update()
     31     {
     32 
     33 
     34         //获取控制的方向, 上下左右,   
     35         float KeyVertical = Input.GetAxis("Vertical");
     36         float KeyHorizontal = Input.GetAxis("Horizontal");
     37         //Debug.Log("keyVertical" + KeyVertical);
     38         //Debug.Log("keyHorizontal" + KeyHorizontal);
     39         if (KeyVertical <0)
     40         {
     41             setHeroState(HERO_DOWN);
     42         }
     43         else if (KeyVertical >0)
     44         {
     45             setHeroState(HERO_UP);
     46         }
     47 
     48         if (KeyHorizontal >0)
     49         {
     50             setHeroState(HERO_RIGHT);
     51         }
     52         else if (KeyHorizontal <0)
     53         {
     54             setHeroState(HERO_LEFT);
     55         }
     56 
     57 
     58         
     59         //得到正在播放的动画状态
     60         AnimatorStateInfo info = PlayerAnimator.GetCurrentAnimatorStateInfo(0);
     61 
     62         //如果没有按下方向键且状态不为walk时播放走路动画
     63         if (KeyVertical != 0 || KeyHorizontal != 0  && !info.IsName("Walk"))
     64         {
     65             PlayerAnimator.Play("Walk");
     66         }
     67         //否则如果按下方向键且状态为walk时播放静止动画  
     68         else if((KeyVertical == 0 && KeyHorizontal == 0 && info.IsName("Walk") ))
     69         {
     70             PlayerAnimator.Play("Idle");
     71         }
     72 
     73         //这里设定是玩家静止时隔2s会摆动一次
     74         if (KeyVertical == 0 && KeyHorizontal == 0)
     75         {
     76             //当玩家静止时,FreakTime才会计时
     77             if (info.IsName("Idle"))
     78             {   
     79                 FreakTime -= Time.deltaTime;
     80                 if (FreakTime <= 0)
     81                 {
     82                     Debug.Log(FreakTime);
     83                     FreakTime = 2;
     84                     //FreakingOut设置为播放后自动退出到idle
     85                     PlayerAnimator.Play("FreakingOut ");
     86                 }
     87             }
     88         }
     89 
     90 
     91     }
     92  
     93     
     94     void setHeroState(int newState)
     95     {
     96         //根据当前人物方向与上一次备份的方向计算出模型旋转的角度  
     97         int rotateValue = (newState - state) * 90;
     98         Vector3 transformValue = new Vector3();
     99 
    100         //播放行走动画  
    101      
    102         //模型移动的位置数值  
    103         switch (newState)
    104         {
    105             case HERO_UP:
    106                 transformValue = Vector3.forward * Time.deltaTime;
    107                 break;
    108             case HERO_DOWN:
    109                 transformValue = (-Vector3.forward) * Time.deltaTime;
    110                 break;
    111             case HERO_LEFT:
    112                 transformValue = Vector3.left * Time.deltaTime;
    113                 break;
    114             case HERO_RIGHT:
    115                 transformValue = (-Vector3.left) * Time.deltaTime;
    116                 break;
    117         }
    118 
    119         transform.Rotate(Vector3.up, rotateValue);
    120         //移动人物  
    121         transform.Translate(transformValue * moveSpeed, Space.World);
    122         state = newState;
    123     }
    124    
    125      
    126 }  
    127  
    View Code

  • 相关阅读:
    小甲鱼与客服的对话,代码与解析
    (role,line_spoken) = each_line.split(':',2) 小甲鱼python 一个任务
    python open() 方法 No such file or directory: 应该怎么解决
    并发编程
    并发编程
    并发编程
    并发编程
    并发编程
    并发编程
    并发编程
  • 原文地址:https://www.cnblogs.com/ninomiya/p/6671315.html
Copyright © 2011-2022 走看看