Unity2D跳跃/下落动画异常问题求助
2D跳跃动画播放异常问题
我正在跟随YouTuber Antarsoft的2D移动与动画教程,完成跳跃动画环节时,完全按照教程操作后,跳跃状态下动画播放异常。尝试关闭动画循环,但仅导致动画无法播放,未找到其他有效解决方案,同类帖子的解决方法也与我的代码不匹配。
问题表现
跳跃时动画播放不符合预期(原视频展示异常状态)
PlayerMovement脚本代码
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { const float groundCheckRadius = 0.2f; const float overheadCheckRadius = 0.2f; [SerializeField] float speed = 1; [SerializeField] float jumpPower = 65; Rigidbody2D rb; Animator animator; [SerializeField]Collider2D standingCollider; [SerializeField] Transform groundCheckCollider; [SerializeField] Transform overheadCheckCollider; [SerializeField] LayerMask groundLayer; float horizontalValue; float runSpeedModifier = 2f; float crouchSpeedModifier = 0.5f; [SerializeField] bool isGrounded; bool jump; [SerializeField] bool crouchPressed; bool isRunning; bool facingRight = true; private void Awake() { rb = GetComponent<Rigidbody2D>(); animator = GetComponent<Animator>(); } // Update is called once per frame void Update() { //Set they yVelocity in the animator animator.SetFloat("yVelocity", rb.velocity.y); horizontalValue = Input.GetAxisRaw("Horizontal"); if (Input.GetKeyDown(KeyCode.LeftShift)) isRunning = true; if (Input.GetKeyUp(KeyCode.LeftShift)) isRunning = false; if (Input.GetButtonDown("Jump")) { jump = true; animator.SetBool("Jump", true); } else if(Input.GetButtonUp("Jump")) jump = false; if (Input.GetKeyDown(KeyCode.LeftShift)) isRunning = true; if (Input.GetKeyUp(KeyCode.LeftShift)) isRunning = false; if (Input.GetButtonDown("Crouch")) crouchPressed = true; else if (Input.GetButtonUp("Crouch")) crouchPressed = false; } void FixedUpdate() { GroundCheck(); Move(horizontalValue, jump, crouchPressed); } void GroundCheck() { isGrounded = false; Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheckCollider.position, groundCheckRadius, groundLayer); if (colliders.Length > 0) isGrounded = true; animator.SetBool("Jump", !isGrounded); } void Move(float dir,bool jumpFlag,bool crouchFlag) { #region jump and crouch if (!crouchFlag) { if (Physics2D.OverlapCircle(overheadCheckCollider.position,overheadCheckRadius,groundLayer)) crouchFlag = true; } if (isGrounded) { standingCollider.enabled = !crouchFlag; //If the player is grounded press space to jump if (jumpFlag) { //isGrounded = false; jumpFlag = false; //Add jump force rb.AddForce(new Vector2(0f, jumpPower)); } } animator.SetBool("Crouch", crouchFlag); #endregion #region move and run //Set value of x using dir and speed float xVal = dir * speed * 100 * Time.fixedDeltaTime; if (isRunning) xVal *= runSpeedModifier; if (crouchFlag) xVal *= crouchSpeedModifier; //Create Vec2 for the Velocity Vector2 targetVelocity = new Vector2(xVal, rb.velocity.y); //Set the players Velocity rb.velocity = targetVelocity; //If looking right and press left, turn left if (facingRight && dir < 0) { transform.localScale = new Vector3(transform.localScale.x * -1, transform.localScale.y, transform.localScale.z); facingRight = false; } //If looking left and press right, turn to the right else if (!facingRight && dir > 0) { transform.localScale = new Vector3(transform.localScale.x * -1, transform.localScale.y, transform.localScale.z); facingRight = true; } Debug.Log(rb.velocity.x); animator.SetFloat("xVelocity", Mathf.Abs(rb.velocity.x)); #endregion } }
内容的提问来源于stack exchange,提问作者Mohammed Hussain
相关产品推荐
相关产品推荐

