[英]My player is switching between animations when it is not programed to
當我的播放器移動時,當我按住 D 時,animation 在空閑和行走之間切換。我玩了 animation,它看起來不錯,但是當我播放過渡時,它開始空閑並切換到行走。 我曾嘗試創建一個新項目並再次執行此操作,但它做同樣的事情。 有誰知道如何解決這一問題?
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player: MonoBehaviour { [SerializeField] float moveForce = 10f; [SerializeField] float jumpForce = 11f; float movementX; Rigidbody2D myBody; Animator Anim; private SpriteRenderer sr; string WALK_ANIMATION = "Walk"; private void Update() { playerMoveKeyboard(); animatePlayer(); } private void Awake() { myBody = GetComponent<Rigidbody2D>(); Anim = myBody.GetComponent<Animator>(); sr = GetComponent<SpriteRenderer>(); } void playerMoveKeyboard() { movementX = Input.GetAxisRaw("Horizontal"); transform.position += new Vector3(movementX, 0f, 0f) * moveForce * Time.deltaTime; } void animatePlayer() { if(movementX > 0) { Anim.SetBool(WALK_ANIMATION, true); sr.flipX = false; } else if (movementX < 0) { Anim.SetBool(WALK_ANIMATION, true); sr.flipX = true; } else { Anim.SetBool(WALK_ANIMATION, false); } } }
不確定我是否從 gif 中正確看到了問題,但你不能關閉“有退出時間”並將轉換持續時間更改為 0 嗎?
不確定,但使用浮點數而不是布爾值來決定何時播放 animation 可能會解決這個問題。 這就是我一直在做的事情,從來沒有遇到任何問題。
通過在動畫器中創建一個名為“速度”的新參數來做到這一點。 將條件從空閑到步行更改為Speed greater 0.01
並將步行到空閑更改為Speed less 0.01
或其他一些較小的數字。
在代碼中,您可以通過添加以下內容將“速度”更改為播放器速度:
anim.setFloat("Speed", Mathf.Abs(movementX))
更新播放器移動腳本中的 function。
您還應該從Anim.SetBool()
中刪除animatePlayer()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.