娱乐建设网站,孝感的网站建设,wordpress侧边栏作者,微信小程序 开发教程通常对俯视角2d游戏的角色移动我们使用简单2d混合树的方式,但是其不移动时的朝向该如何定义#xff1f; 十分简单#xff1a;移动和不移动之间形成逻辑自锁 详细说明思路就是再创建一个简单2d混合树 定义其N方向的idle 并用lastDirc二维向量保存玩家输入#xff0c;当玩家输… 通常对俯视角2d游戏的角色移动我们使用简单2d混合树的方式,但是其不移动时的朝向该如何定义 十分简单移动和不移动之间形成逻辑自锁 详细说明思路就是再创建一个简单2d混合树 定义其N方向的idle 并用lastDirc二维向量保存玩家输入当玩家输入为0时就不进改变lastDirc的函数块用该变量去改变idle混合树状态即可 请看整体代码
using System;
using UnityEngine;[Serializable]
public class PlayerCtrl : MonoBehaviour {private static PlayerCtrl instance;public static PlayerCtrl Instance instance;public Vector2 moveDir;public Vector2 lastDir;[SerializeField]private float moveSpeed;public Rigidbody2D playerRb;private void Awake() {if (instance null){ instance this;}else{ Destroy(instance);}playerRb GetComponentRigidbody2D();}void Start() {moveSpeed 0.1f;}void Update() {GetInput();}private void FixedUpdate() {FixedMove();}public void GetInput(){moveDir new Vector2(Input.GetAxisRaw(Horizontal), Input.GetAxisRaw(Vertical));}private void FixedMove() {playerRb.MovePosition((Vector2)this.playerRb.position moveDir.normalized * moveSpeed);}
}using UnityEngine;public class PlayerAm : MonoBehaviour
{ private Animator playerAm;private void Awake() {playerAm GetComponentAnimator();}// Start is called once before the first execution of Update after the MonoBehaviour is createdvoid Start(){}// Update is called once per framevoid Update(){PlayerAmCtrl();}public void PlayerAmCtrl(){if (PlayerCtrl.Instance.moveDir ! Vector2.zero) {PlayerCtrl.Instance.lastDir PlayerCtrl.Instance.moveDir;playerAm.SetBool(isMoving, true);playerAm.SetFloat(X, PlayerCtrl.Instance.moveDir.x);playerAm.SetFloat(Y, PlayerCtrl.Instance.moveDir.y);}else {playerAm.SetBool(isMoving, false);playerAm.SetFloat(Ix, PlayerCtrl.Instance.lastDir.x);playerAm.SetFloat(Iy, PlayerCtrl.Instance.lastDir.y);}}
}为什么这么简单的事情还需要记录呢 因为我一直在寻找不这么做就可以解决的方式但是似乎涉及到了这个2d简单混合树后再只想通过代码去解决是很难的 如果是简单的一维向量锁方向就行了希望大佬看到此篇后有更加简单的方法私信我