[图文教程] 【原创】unity native2D教程第六课:Hero的状态机

我们在assets里创建一个Animator Charactercontrol,命名为Character,这就是我们Hero的组件Animator需要添加的controller。
       首先我们在Base Layer创建五个State(Idle,Run,Jump,Death,Falling),然后把我们前面创建好的animation Clip赋予到Motion参数,然后再左下角添加4个参数,分别是float类型的speed,Trigger类型的Jump,Shoot,Die。

       在Idle=Run状态我们通过speed连接起来(在inspector视图的conditions选项上选择speed,从idle到Run为greater,反之则为less)

       Run=Jump状态我们通过Jump连接起来(从Run到Jump我们只要选condition为Jump,而从Jump到Run我们则要选择condition为speed,greater大于0.1。还有exit time为0.58)。

        Idle=Jump状态通过Jump连接。

        Death和Falling状态我们在any State中连接起来。

        详情如下图:



      下面我们来做Shooting的状态设置,这里我们在右上角出增加一个层shooting层。为什么要这样设置呢,因为我们需要hero可以一边移动同时一边射击,而如果我们把shooting状态放到Base Layer层的话,hero会先停止当前的状态动画才去进行射击动画。这显然不是我们想要的效果,我们需要hero能够一个移动的同时一边可以进行射击,两者互不影响。在这里增加一个shooting就起到了这个作用,我们把weight(权重)设为1,Blending选项为Override。这样hero就可以达到我们想要的效果。)(从Any State到shoot状态的触发器设置condition为shoot)如下图。

 


关于状态机这部分如果大家看上面的图不清晰,可以看原来的项目。

        下面我们进入到脚本控制部分,开始进行hero的脚本编写。创建一个脚本PlayerControl,挂在Hero上。下面是移动的代码:

        using UnityEngine;

        using System.Collections;


        public class PlayerControl : MonoBehaviour

        {

        [HideInInspector]

         public bool facingRight = true;

         [HideInInspector]

         public bool jump = false;

         public float moveForce = 365f;

         public float maxSpeed = 5f;

        void Awake()

{

          anim = GetComponent<Animator>();

         }

        void FixedUpdate ()

         {

          // Cache the horizontal input.

          float h = Input.GetAxis("Horizontal");

                //水平方向设置animator参数一个Horizontal的绝对值

          // The Speed animator parameter is set to the absolute value of the horizontal input.

          anim.SetFloat("Speed", Mathf.Abs(h));


          // If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...

          if(h * rigidbody2D.velocity.x < maxSpeed)

           // ... add a force to the player.方向不变,没有到达最大速度时赋予player力

           rigidbody2D.AddForce(Vector2.right * h * moveForce);


          // If the player's horizontal velocity is greater than the maxSpeed...

          if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)

           // ... set the player's velocity to the maxSpeed in the x axis.当到达最大速度,重新赋予速度

           rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);


          // If the input is moving the player right and the player is facing left...//如果player向右移而方向没有向右,转向

          if(h > 0 && !facingRight)

           // ... flip the player.

           Flip();

          // Otherwise if the input is moving the player left and the player is facing right...

          else if(h < 0 && facingRight)

           // ... flip the player.

           Flip();


         }

        }

        void Flip ()

         {

          // Switch the way the player is labelled as facing.

          facingRight = !facingRight;


          // Multiply the player's x local scale by -1.

          Vector3 theScale = transform.localScale;

          theScale.x *= -1;

          transform.localScale = theScale;

         }


        代码挺简单的,我们通过给我们的Hero调用AddForce方法,赋予Hero一个方向上的力使其移动,同时播放移动的动画。

更多精彩关注狗刨学习网: http://www.gopedu.com/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值