烟台制作网站有哪些,无忧网站源码,淄博网站建设选哪家,英文定机票网站建设提示#xff1a;文章写完后#xff0c;目录可以自动生成#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、通过InControl插件实现绑定玩家输入二、制作小骑士移动和空闲动画 1.制作动画2.玩家移动和翻转图像3.状态机思想实现动画切换总结 前言
好久没来CSDN看看文章写完后目录可以自动生成如何生成可参考右边的帮助文档 文章目录 前言一、通过InControl插件实现绑定玩家输入二、制作小骑士移动和空闲动画 1.制作动画2.玩家移动和翻转图像3.状态机思想实现动画切换总结 前言
好久没来CSDN看看突然看到前两年自己写的文章从零开始制作空洞骑士只做了一篇就突然烂尾了刚好最近开始学习做Unity我决定重启这个项目从零开始制作空洞骑士第一集我们导入了素材和远程git管理项目OK这期我们就从通过InControl插件实现绑定玩家输入以及制作小骑士移动和空闲动画。 一、通过InControl插件实现绑定玩家输入
其实这一部挺难的因为InControl插件你在网上绝对不超过五个视频资料但没办法空洞骑士就是用这个来控制键盘控制器输入的为了原汁原味就只能翻一下InControl提供的Examples示例里学习。
学习完成后直接来看我写的InputHandler.cs和GameManager.cs 在GameManager.cs中我们暂且先只用实现一个单例模式
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class GameManager : MonoBehaviour
{private static GameManager _instance;public static GameManager instance{get{if(_instance null){_instance FindObjectOfTypeGameManager();}if (_instance null){Debug.LogError(Couldnt find a Game Manager, make sure one exists in the scene.);}else if (Application.isPlaying){DontDestroyOnLoad(_instance.gameObject);}return _instance;}}private void Awake(){if(_instance ! this){_instance this;DontDestroyOnLoad(this);return;}if(this ! _instance){Destroy(gameObject);return;}}}在来到InputHandler.cs之前我们还需要创建映射表HeroActions
using System;
using InControl;public class HeroActions : PlayerActionSet
{public PlayerAction left;public PlayerAction right;public PlayerAction up;public PlayerAction down;public PlayerTwoAxisAction moveVector;public HeroActions(){left CreatePlayerAction(Left);left.StateThreshold 0.3f;right CreatePlayerAction(Right);right.StateThreshold 0.3f;up CreatePlayerAction(Up);up.StateThreshold 0.3f;down CreatePlayerAction(Down);down.StateThreshold 0.3f;moveVector CreateTwoAxisPlayerAction(left, right, up, down);moveVector.LowerDeadZone 0.15f;moveVector.UpperDeadZone 0.95f;}
}OK到了最关键的InputHandler.cs了
using System;
using System.Collections;
using System.Collections.Generic;
using GlobalEnums;
using InControl;
using UnityEngine;public class InputHandler : MonoBehaviour
{public InputDevice gameController;public HeroActions inputActions;public void Awake(){inputActions new HeroActions();}public void Start(){MapKeyboardLayoutFromGameSettings();if(InputManager.ActiveDevice ! null InputManager.ActiveDevice.IsAttached){}else{gameController InputDevice.Null;}Debug.LogFormat(Input Device set to {0}., new object[]{gameController.Name});}
//暂时没有GameSettings后续会创建的这里是指将键盘按键绑定到HeroActions 中private void MapKeyboardLayoutFromGameSettings(){AddKeyBinding(inputActions.up, W);AddKeyBinding(inputActions.down, S);AddKeyBinding(inputActions.left, A);AddKeyBinding(inputActions.right, D);}private static void AddKeyBinding(PlayerAction action, string savedBinding){Mouse mouse Mouse.None;Key key;if (!Enum.TryParse(savedBinding, out key) !Enum.TryParse(savedBinding, out mouse)){return;}if (mouse ! Mouse.None){action.AddBinding(new MouseBindingSource(mouse));return;}action.AddBinding(new KeyBindingSource(new Key[]{key}));}}给我们的小骑士创建一个HeroController.cs检测输入最关键的是一行代码
move_input inputHandler.inputActions.moveVector.Vector.x;
using System;
using System.Collections;
using System.Collections.Generic;
using HutongGames.PlayMaker;
using GlobalEnums;
using UnityEngine;public class HeroController : MonoBehaviour
{public ActorStates hero_state;public ActorStates prev_hero_state;public bool acceptingInput true;public float move_input;public float RUN_SPEED 5f;private Rigidbody2D rb2d;private BoxCollider2D col2d;private GameManager gm;private InputHandler inputHandler;private void Awake(){SetupGameRefs();}private void SetupGameRefs(){rb2d GetComponentRigidbody2D();col2d GetComponentBoxCollider2D();gm GameManager.instance;inputHandler gm.GetComponentInputHandler();}void Start(){}void Update(){orig_Update();}private void orig_Update(){if (hero_state ActorStates.no_input){}else if(hero_state ! ActorStates.no_input){LookForInput();}}private void FixedUpdate(){if (hero_state ! ActorStates.no_input){Move(move_input);}}private void Move(float move_direction){if(acceptingInput){rb2d.velocity new Vector2(move_direction * RUN_SPEED, rb2d.velocity.y);}}private void LookForInput(){if (acceptingInput){move_input inputHandler.inputActions.moveVector.Vector.x;}}
}[Serializable]
public class HeroControllerStates
{public bool facingRight;public bool onGround;public HeroControllerStates(){facingRight true;onGround false;}
}记得一句话FixedUpdate()处理物理移动Update()处理逻辑
二、制作小骑士移动和空闲动画
1.制作动画 素材找到Idle和Walk文件夹创建两个同名animationsprite往上面一放自己就做好了。 可能你注意到我没有给这两个动画连线其实是我想做个动画状态机通过核心代码 animator.Play来管理动画的切换。 2.玩家移动和翻转图像
我们还需要给小骑士添加更多的功能比如翻转图像
using System;
using System.Collections;
using System.Collections.Generic;
using HutongGames.PlayMaker;
using GlobalEnums;
using UnityEngine;public class HeroController : MonoBehaviour
{public ActorStates hero_state;public ActorStates prev_hero_state;public bool acceptingInput true;public float move_input;public float RUN_SPEED 5f;private Rigidbody2D rb2d;private BoxCollider2D col2d;private GameManager gm;private InputHandler inputHandler;public HeroControllerStates cState;private HeroAnimatorController animCtrl;private void Awake(){SetupGameRefs();}private void SetupGameRefs(){if (cState null)cState new HeroControllerStates();rb2d GetComponentRigidbody2D();col2d GetComponentBoxCollider2D();animCtrl GetComponentHeroAnimatorController();gm GameManager.instance;inputHandler gm.GetComponentInputHandler();}void Start(){}void Update(){orig_Update();}private void orig_Update(){if (hero_state ActorStates.no_input){}else if(hero_state ! ActorStates.no_input){LookForInput();}}private void FixedUpdate(){if (hero_state ! ActorStates.no_input){Move(move_input);if(move_input 0f !cState.facingRight ){FlipSprite();}else if(move_input 0f cState.facingRight){FlipSprite();}}}private void Move(float move_direction){if (cState.onGround){SetState(ActorStates.grounded);}if(acceptingInput){rb2d.velocity new Vector2(move_direction * RUN_SPEED, rb2d.velocity.y);}}public void FlipSprite(){cState.facingRight !cState.facingRight;Vector3 localScale transform.localScale;localScale.x * -1f;transform.localScale localScale;}private void LookForInput(){if (acceptingInput){move_input inputHandler.inputActions.moveVector.Vector.x;}}/// summary/// 设置玩家的ActorState的新类型/// /summary/// param namenewState/paramprivate void SetState(ActorStates newState){if(newState ActorStates.grounded){if(Mathf.Abs(move_input) Mathf.Epsilon){newState ActorStates.running;}else{newState ActorStates.idle;}}else if(newState ActorStates.previous){newState prev_hero_state;}if(newState ! hero_state){prev_hero_state hero_state;hero_state newState;animCtrl.UpdateState(newState);}}private void OnCollisionEnter2D(Collision2D collision){if(collision.gameObject.layer LayerMask.NameToLayer(Wall)){cState.onGround true;}}private void OnCollisionStay2D(Collision2D collision){if (collision.gameObject.layer LayerMask.NameToLayer(Wall)){cState.onGround true;}}private void OnCollisionExit2D(Collision2D collision){if (collision.gameObject.layer LayerMask.NameToLayer(Wall)){cState.onGround false;}}
}[Serializable]
public class HeroControllerStates
{public bool facingRight;public bool onGround;public HeroControllerStates(){facingRight true;onGround false;}
}创建命名空间GlobalEnums创建一个新的枚举类型
using System;namespace GlobalEnums
{public enum ActorStates{grounded,idle,running,airborne,wall_sliding,hard_landing,dash_landing,no_input,previous}
}3.状态机思想实现动画切换
有了这些我们就可以有效控制动画切换创建一个新的脚本给Player 可以看到我们创建了两个属性记录当前的actorState和上一个actorState来实现动画切换后面会用到的
using System;
using GlobalEnums;
using UnityEngine;public class HeroAnimatorController : MonoBehaviour
{private Animator animator;private AnimatorClipInfo[] info;private HeroController heroCtrl;private HeroControllerStates cState;private string clipName;private float currentClipLength;public ActorStates actorStates { get; private set; }public ActorStates prevActorState { get; private set; }private void Start(){animator GetComponentAnimator();heroCtrl GetComponentHeroController();actorStates heroCtrl.hero_state;PlayIdle();}private void Update(){UpdateAnimation();}private void UpdateAnimation(){//info animator.GetCurrentAnimatorClipInfo(0);//currentClipLength info[0].clip.length;//clipName info[0].clip.name;if(actorStates ActorStates.no_input){//TODO:}else if(actorStates ActorStates.idle){//TODO:PlayIdle();}else if(actorStates ActorStates.running){PlayRun();}}private void PlayRun(){animator.Play(Run);}public void PlayIdle(){animator.Play(Idle);}public void UpdateState(ActorStates newState){if(newState ! actorStates){prevActorState actorStates;actorStates newState;}}
}总结
最后给大伙看看效果怎么样可以看到运行游戏后cStatehero_state和prev_hero_state都没有问题,动画也正常播放 累死我了我去睡个觉顺便上传到githubOK大伙晚安醒来接着更新。 文章转载自: http://www.morning.yfpnl.cn.gov.cn.yfpnl.cn http://www.morning.kxsnp.cn.gov.cn.kxsnp.cn http://www.morning.wkmyt.cn.gov.cn.wkmyt.cn http://www.morning.crrmg.cn.gov.cn.crrmg.cn http://www.morning.ndzhl.cn.gov.cn.ndzhl.cn http://www.morning.htsrm.cn.gov.cn.htsrm.cn http://www.morning.nknt.cn.gov.cn.nknt.cn http://www.morning.jzgxp.cn.gov.cn.jzgxp.cn http://www.morning.wktbz.cn.gov.cn.wktbz.cn http://www.morning.rczrq.cn.gov.cn.rczrq.cn http://www.morning.ljbch.cn.gov.cn.ljbch.cn http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn http://www.morning.qlwfz.cn.gov.cn.qlwfz.cn http://www.morning.hxsdh.cn.gov.cn.hxsdh.cn http://www.morning.c7497.cn.gov.cn.c7497.cn http://www.morning.fxjnn.cn.gov.cn.fxjnn.cn http://www.morning.kpzrf.cn.gov.cn.kpzrf.cn http://www.morning.hyhqd.cn.gov.cn.hyhqd.cn http://www.morning.nxtgb.cn.gov.cn.nxtgb.cn http://www.morning.fglth.cn.gov.cn.fglth.cn http://www.morning.psdsk.cn.gov.cn.psdsk.cn http://www.morning.ccdyc.cn.gov.cn.ccdyc.cn http://www.morning.qpsdq.cn.gov.cn.qpsdq.cn http://www.morning.wqfj.cn.gov.cn.wqfj.cn http://www.morning.rhph.cn.gov.cn.rhph.cn http://www.morning.mfxcg.cn.gov.cn.mfxcg.cn http://www.morning.clgbb.cn.gov.cn.clgbb.cn http://www.morning.dbphz.cn.gov.cn.dbphz.cn http://www.morning.rrpsw.cn.gov.cn.rrpsw.cn http://www.morning.bmmyx.cn.gov.cn.bmmyx.cn http://www.morning.bwkzn.cn.gov.cn.bwkzn.cn http://www.morning.wpydf.cn.gov.cn.wpydf.cn http://www.morning.bfmq.cn.gov.cn.bfmq.cn http://www.morning.xysdy.cn.gov.cn.xysdy.cn http://www.morning.kqgqy.cn.gov.cn.kqgqy.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.pypbz.cn.gov.cn.pypbz.cn http://www.morning.bflws.cn.gov.cn.bflws.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.lfgql.cn.gov.cn.lfgql.cn http://www.morning.tfznk.cn.gov.cn.tfznk.cn http://www.morning.pcqdf.cn.gov.cn.pcqdf.cn http://www.morning.pffqh.cn.gov.cn.pffqh.cn http://www.morning.kjyqr.cn.gov.cn.kjyqr.cn http://www.morning.dxxnq.cn.gov.cn.dxxnq.cn http://www.morning.bpmdx.cn.gov.cn.bpmdx.cn http://www.morning.xkzmz.cn.gov.cn.xkzmz.cn http://www.morning.chhhq.cn.gov.cn.chhhq.cn http://www.morning.smjyk.cn.gov.cn.smjyk.cn http://www.morning.mlntx.cn.gov.cn.mlntx.cn http://www.morning.slzkq.cn.gov.cn.slzkq.cn http://www.morning.frcxx.cn.gov.cn.frcxx.cn http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.kyfnh.cn.gov.cn.kyfnh.cn http://www.morning.rgnq.cn.gov.cn.rgnq.cn http://www.morning.jnvivi.com.gov.cn.jnvivi.com http://www.morning.mnbcj.cn.gov.cn.mnbcj.cn http://www.morning.kgrwh.cn.gov.cn.kgrwh.cn http://www.morning.mtrrf.cn.gov.cn.mtrrf.cn http://www.morning.rfdqr.cn.gov.cn.rfdqr.cn http://www.morning.cwfkm.cn.gov.cn.cwfkm.cn http://www.morning.pmghz.cn.gov.cn.pmghz.cn http://www.morning.trqsm.cn.gov.cn.trqsm.cn http://www.morning.c7622.cn.gov.cn.c7622.cn http://www.morning.pabxcp.com.gov.cn.pabxcp.com http://www.morning.jbnss.cn.gov.cn.jbnss.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.mpscg.cn.gov.cn.mpscg.cn http://www.morning.nbqwr.cn.gov.cn.nbqwr.cn http://www.morning.wnkjb.cn.gov.cn.wnkjb.cn http://www.morning.jqbpn.cn.gov.cn.jqbpn.cn http://www.morning.sjqml.cn.gov.cn.sjqml.cn http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn http://www.morning.zcwwb.cn.gov.cn.zcwwb.cn http://www.morning.slwfy.cn.gov.cn.slwfy.cn http://www.morning.lctrz.cn.gov.cn.lctrz.cn http://www.morning.kjyfq.cn.gov.cn.kjyfq.cn http://www.morning.routalr.cn.gov.cn.routalr.cn