Unity中角色向左翻转动画失效问题排查求助
Unity角色向左移动动画无法正常显示问题
我在Unity开发中,通过动画录制功能制作了角色向左移动、中途停止的动画,并将这些动画整合为混合树用于左右转向逻辑。但实际运行时,角色向左移动的翻转画面无法正常显示。
我已经在LateUpdate方法中设置了Animator的DirX、DirY和Idle参数,混合树的动画参数匹配规则为:向左对应DirX=-1,向右对应DirX=1。在Animator预览窗口中,Sprite可以正常翻转,但进入游戏运行后效果不符合预期。
以下是完整的PlayerManager代码:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerManager : MonoBehaviour { Coroutine playerMoveRoutine; public float speed = 500f; public float runSpeed = 200f; public GameObject player; public GameObject rightArrow; public GameObject leftArrow; public GameObject[] backgrounds; public GameObject[] Borders; private Vector2 movement; //Use the direction key to move private bool isRunning; private Animator anim; private Rigidbody2D rb; private void Start() { anim = player.GetComponent<Animator>(); rb = player.GetComponent<Rigidbody2D>(); } private void Update() { // Get input movement.x = Input.GetAxisRaw("Horizontal"); movement.y = Input.GetAxisRaw("Vertical"); isRunning = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift); if (Input.GetMouseButtonDown(0)) // Left mouse click detection { Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero); if (hit.collider != null) { if (hit.collider.gameObject == rightArrow) { int clickX = 1; // If we're already moving, cancel that coroutine so the two don't execute at the same time if (playerMoveRoutine != null) { StopCoroutine(playerMoveRoutine); } playerMoveRoutine = StartCoroutine(ArrowClick(clickX)); } else if (hit.collider.gameObject == leftArrow) { int clickX = -1; // If we're already moving, cancel that coroutine so the two don't execute at the same time if (playerMoveRoutine != null) { StopCoroutine(playerMoveRoutine); } playerMoveRoutine = StartCoroutine(ArrowClick(clickX)); } } } } public IEnumerator ArrowClick(int _clickX) { float speed = 200; Vector2 target = new Vector2(500 * _clickX, 0); while (Vector2.Distance(player.transform.position, target) > 0.05f) { // Changed from hardcoded "10" to rather be based on the "Time.deltaTime": enables framerate independence (player will move the same amount in a second, regardless of FPS) player.transform.position = Vector2.MoveTowards(player.transform.position, target, speed * Time.deltaTime); // Tells the game to wait a frame before continuing execution yield return null; } } private void FixedUpdate() { // Move the character float currentSpeed = isRunning ? speed * runSpeed : speed; Vector2 velocity = movement.normalized * currentSpeed; rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime); } private void LateUpdate() { // Update animator anim.SetFloat("DirX", movement.x); anim.SetFloat("DirY", movement.y); anim.SetBool("Idle", movement.magnitude == 0); } }
相关截图:
内容的提问来源于stack exchange,提问作者codebetatester
相关产品推荐
相关产品推荐






