zoukankan      html  css  js  c++  java
  • [Unity]-黑魂复刻-动画 001

    为角色移动做准备

    现在的游戏大都是通过上下左右来控制角色移动(w s a d),我们现在也一样。

    1.创建一个 PlayerInput脚本,并声明控制键

    public KeyCode keyUp = KeyCode.W;
    public KeyCode keyDown = KeyCode.S;
    public KeyCode keyLeft = KeyCode.A;
    public KeyCode keyRight = KeyCode.D;
    

    2.需要一个约束输入的控制开关

    public bool inputEnable = true;//保持打开
    

    3.为角色的方向做准备,声明变量

    //角色的方向
    public float Dup;
    pulbic float Dright;
    
    //目标方向
    private float targetDup;
    private float targetDright;
    
    //SmoothDamp方法中传出的值
    private float upVel;
    private float rightVel;
    
    

    在 Update中加入

    if(!inputEnable)return;//没有输入就返回
    
    // 目标朝向
    targetDup = (Input.GetKey(keyUp) ? 1f : 0f) - (Input.GetKey(keyDown) ? 1.0f : 0f);
    targetDright = (Input.GetKey(keyRight) ? 1f : 0f) - (Input.GetKey(keyLeft) ? 1.0f : 0f);
    
    //前进、后退,(SmoothDamp() 缓缓的转向某处)
    Dup = Mathf.SmoothDamp(Dup, targetDup, ref velocityDup, 0.1f);
    //向左、向右
    Dright = Mathf.SmoothDamp(Dright, targetDright, ref velocityDright, 0.1f);
    
    

    现在,基本的控制已经做好了,接下来就该控制角色的动画了

    创建一个PlayerCtrl脚本用来控制角色的动画和方向
    先声明一些常用的变量:

    public GameObject model;//需要控制的模型(通常为动画的父类)
    public Animator ani;//获取模型的动画
    public PlayerInput pi;//控制角色的输入
    

    在Awake中获取到定义的变量:

    ani = model.GetComponent<Animator>();
    pi = GetComponent<PlayerInput>();
    

    现在,终于可以在Update中控制角色的动画了

     ani.SetFloat("forward", Mathf.Sqrt(Dup * Dup + Dright * Dright));//播放动画,(forward 是动画层级的名称)
    

    使用键盘(wsad)便可以控制角色的动画播放了,但模型的方向却没有发生变化,不用着急,马上解决这个问题!!!!

    简单重构代码,改变角色方向

    首先,来理一下大概的思路:

    PlayerInput PlayerCtrl
    获取到用户的输入,并传递给PlayerCtrl 专门用于控制角色的动画和方向

    直接贴上完整的代码吧:
    PlayerCtlr:

    public class PlayerCtrl : MonoBehaviour {
        public GameObject model;
        private Animator ani;
    
        private PlayerInput pi;
    
        private void Awake() {
            ani = model.GetComponent<Animator>();
            pi = GetComponent<PlayerInput>();
        }
    
        private void Update() {
            ani.SetFloat("forward", pi.Dmag);
            if (pi.Dmag > 0.1f) {
                model.transform.forward = pi.Dvec;
            }
        }
    
    }
    

    PlayerInput:

    public class PlayerInput : MonoBehaviour {
        public KeyCode keyUp = KeyCode.W;
        public KeyCode keyDown = KeyCode.S;
        public KeyCode keyLeft = KeyCode.A;
        public KeyCode keyRight = KeyCode.D;
    
        #region 设置方向
        /// <summary>
        /// 向上还是向下,1=上,0=下
        /// </summary>
        public float Dup;
        /// <summary>
        /// 向还是向右,0=左,1=右
        /// </summary>
        public float Dright;
        /// <summary>
        /// 输入开关
        /// </summary>
        public bool inputEnable = true;
    
        //目标方向
        private float targetDup;
        private float targetDright;
        //转动速度
        private float DupVelocity;
        private float DrightVelocity;
        
        [HideInInspector]
        public float Dmag;//角色动画
        [HideInInspector]public Vector3 Dvec;//角色方向
    
        #endregion
    
        private void Start() {
            DSMonoMgr.Instance.AddUpdateListener(OnUpdate);
        }
    
        private void OnUpdate() {
            //设置方向
            targetDup = ((Input.GetKey(keyUp) ? 1.0f : 0f) - (Input.GetKey(keyDown) ? 1.0f : 0f));
            targetDright = ((Input.GetKey(keyRight) ? 1.0f : 0f) - (Input.GetKey(keyLeft) ? 1.0f : 0f));
    
            if (inputEnable == false) return;
    
            Dup = Mathf.SmoothDamp(Dup, targetDup, ref DupVelocity, 0.1f);
            Dright = Mathf.SmoothDamp(Dright, targetDright, ref DrightVelocity, 0.1f);
    
            Dmag = Mathf.Sqrt(Dup * Dup + Dright * Dright);
            Dvec = Dright * transform.right + Dup * transform.forward; 
        }
    }
    
    
  • 相关阅读:
    Axure 8 注册码,市面上很多注册码都不行用,但是这个可以。
    SmartGit Mac、Liunx、Windows过期后破解方法
    iOS蓝牙4.0开发例子
    苹果发布【新开发语言】Swift
    iOS Web开发
    [置顶]JB开发之制作系统级Application
    mac 10.9 dock在多屏幕间移动
    IOS Application Security Testing Cheat Sheet
    看上去很美 国内CDN现状与美国对比
    iOS 企业证书发布app 流程
  • 原文地址:https://www.cnblogs.com/unitysir/p/13710224.html
Copyright © 2011-2022 走看看