赞
踩
在前面介绍了开始界面的创建点击打开链接
在我们创建完主游戏界面的背景后接下来开始完善主角的创建
一.主角的动作
选中Hierarchy窗口中的主角,点中Window->Animation的Create将自动为该对象添加Animator组件
将每帧图片按需要的时间帧拖过去
在Animator窗口内将增加一个动画
接下来点击如下图的加号,可以选择一个类型,这里选择Bool类型
点击idle动画与walk动画的连线
按如下图选择,意思是当触发变量walk为true时,动作walk将播放
动画管理代码列表如下:
AnimController.cs
- using UnityEngine;
- using System.Collections;
-
-
- public class AnimController : MonoBehaviour
- {
- public Animator[] anim;
-
-
- public void PlayIdleAnimation()
- {
- foreach(Animator value in anim)
- {
- value.SetBool("Walk",false);
- }
-
-
- anim [0].SetBool ("Jump",false);
- anim [0].SetBool ("ShootForward",false);
- anim [0].SetBool ("ShootUP",false);
-
-
- }
-
-
-
-
- public void PlayWalkAnimation()
- {
- foreach (Animator value in anim)
- {
- value.SetBool("Walk",true);
- }
- }
-
-
-
-
- public void PlayJumpAnimation()
- {
- foreach (Animator value in anim)
- {
- value.SetTrigger("Jump");
- }
- }
-
-
-
-
- public void PlayTrowAnimation()
- {
- anim [0].SetTrigger ("Throw");
- }
-
-
-
-
- public void PlayAttackAnimation()
- {
- anim [0].SetTrigger ("Attack");
- }
-
-
-
-
- public void PlayShootForwardAnimation()
- {
- anim [0].SetBool ("ShootForward",true);
- }
-
-
-
-
- public void PlayShootUpAnimation()
- {
- anim [0].SetBool ("ShootUP",true);
- }
- }
二.主角的移动管理
给主角添加BoxCollider2D,RigidBody2D,AudioSource组件,以及AnimControlle C#r脚本组件
PlayerController.cs
- using UnityEngine;
- using System.Collections;
-
-
- public enum BulletDirection
- {
- RIGHT,LEFT
- }
-
-
- public class PlayController : MonoBehaviour
- {
- private Rigidbody2D rigidBody;
- private AnimController anim;
- private ShootAnim shoot;
- private GrenadeAnim grenade;
-
-
-
-
- //给钢体一个移动的速度
- public float moveSpeed=4.0f;
-
-
- //给钢体起跳的力
- public float upForce=700.0f;
-
-
- public BulletDirection bdt=BulletDirection.RIGHT;
-
-
- // Use this for initialization
- void Start ()
- {
- rigidBody =GetComponent<Rigidbody2D> ();
- anim = GetComponent<AnimController> ();
- shoot = GetComponent<ShootAnim> ();
- grenade = GetComponent<GrenadeAnim> ();
- }
-
- // Update is called once per frame
- void Update ()
- {
- #region idle状态
- rigidBody.velocity=new Vector2(0,rigidBody.velocity.y);
-
- anim.PlayIdleAnimation();
- #endregion
-
-
-
-
- #region 控制移动
- //获取按键的值,当h小于0时,表示按下A;h值大于0时,表示按下D
- float h = Input.GetAxis ("Horizontal");
-
- if (h>0)
- {
- bdt=BulletDirection.RIGHT;
-
-
- this.transform.rotation=Quaternion.Euler(new Vector3(0,180,0));
- rigidBody.velocity=new Vector2(h*moveSpeed,rigidBody.velocity.y);
-
- anim.PlayWalkAnimation();
- }
-
- if (h<0)
- {
- bdt=BulletDirection.LEFT;
-
-
- this.transform.rotation=Quaternion.Euler(new Vector3(0,0,0));
- rigidBody.velocity=new Vector2(h*moveSpeed,rigidBody.velocity.y);
-
- anim.PlayWalkAnimation();
- }
- #endregion
-
-
-
-
- #region 控制跳跃
- if (Input.GetKeyDown(KeyCode.K) && rigidBody.velocity.y==0)
- {
- rigidBody.AddForce(Vector2.up*upForce);
-
-
- anim.PlayJumpAnimation();
- }
- #endregion
-
-
-
-
-
-
- #region 控制射击
- if(Input.GetKey(KeyCode.J)&&Input.GetKey(KeyCode.W))
- {
- shoot.StartShoot(ShootDirection.UP);
- anim.PlayShootUpAnimation();
- }
- else if(Input.GetKey(KeyCode.J))
- {
- shoot.StartShoot(ShootDirection.FORWARD);
- anim.PlayShootForwardAnimation();
- }
- else
- {
- shoot.StopShoot();
- }
- #endregion
-
-
-
-
- #region 控制扔手榴弹
- if(Input.GetKey(KeyCode.L))
- {
- grenade.StartGrenadeAnim();
-
-
- anim.PlayTrowAnimation();
- }
- else
- {
- grenade.StopGrenadeAnim();
- }
- #endregion
- }
-
-
- #region 获取子弹设计方向(左右)
- public BulletDirection GetCurrentBulletDirection()
- {
- return bdt;
- }
-
- #endregion
- }
这样主角的动画,左右移动,跳跃移动,子弹射击,炸弹投放已经准备好了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。