通常对俯视角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 = GetComponent<Rigidbody2D>();
}
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 = GetComponent<Animator>();
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void 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简单混合树后再只想通过代码去解决是很难的
如果是简单的一维向量锁方向就行了,希望大佬看到此篇后有更加简单的方法私信我