在空中播放完整的跳跃动画时遇到问题
本文关键字:动画 遇到 问题 跳跃 播放 在空中 | 更新日期: 2023-09-27 18:28:45
我试图控制一组角色动画,但我在跳跃动画方面遇到了问题。我的水平行走动画运行良好,但当我按下跳跃按钮时,我希望播放两个跳跃动画中的一个,这取决于玩家是处于空闲状态还是行走状态。
我已经设置了一个bool,它会根据我的角色碰撞器是否接触地面进行更新,并进行检查以确定何时播放跳跃动画。然而,当我当前按下跳跃按钮时,它只播放相应跳跃动画的第一帧,然后直接返回到空闲或行走。当按下跳跃按钮并且角色悬空时,如何播放整个跳跃动画?
using UnityEngine;
using System.Collections;
public class AsherBlackMover : MonoBehaviour {
public float moveSpeed = 3;
public float rotateSpeed = 10;
public Transform graphics;
public SkeletonAnimation skeletonAnimation;
public Vector2 jumpVector;
public bool isGrounded;
// isGrounded Variables
public Transform grounderPosition;
public float grounderRadius;
public LayerMask grounderLayerMask;
private Rigidbody2D asherBlackRB;
private Animator asherBlackAnim;
Quaternion goalRotation = Quaternion.identity;
float xDir;
string currentAnimation = "";
void Start ()
{
// Create a reference to Asher Black Rigidbody2D
asherBlackRB = GetComponent<Rigidbody2D>();
}
void Update ()
{
xDir = Input.GetAxis ("Horizontal") * moveSpeed;
if (xDir > 0 && isGrounded == true) // ------ Walk Right
{
goalRotation = Quaternion.Euler (0, 0, 0);
SetAnimation ("Walk", true);
}
else if (xDir < 0 && isGrounded == true) // ------ Walk Left
{
goalRotation = Quaternion.Euler (0, 180, 0);
SetAnimation ("Walk", true);
}
else if (isGrounded == true) // ------ Idle
{
SetAnimation ("Idle", true);
}
// Jump Button
if (Input.GetKeyDown("space") && isGrounded == true)
{
isGrounded = false;
skeletonAnimation.state.SetAnimation (0, "Idle-Jump", true);
if (xDir > 0 && isGrounded == false)
{
Jump ("Walk-Jump");
}
else if (xDir < 0 && isGrounded == false)
{
Jump ("Walk-Jump");
}
else if (isGrounded == false)
{
Jump ("Idle-Jump");
}
}
// Circle on character that determines when grounded or not
isGrounded = Physics2D.OverlapCircle (grounderPosition.transform.position, grounderRadius, grounderLayerMask);
// Flip character smothly to emulate paper mario effect
graphics.localRotation = Quaternion.Lerp (graphics.localRotation, goalRotation, rotateSpeed * Time.deltaTime);
}
void OnDrawGizmos ()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere (grounderPosition.transform.position, grounderRadius);
}
void SetAnimation (string name, bool loop)
{
if (name == currentAnimation)
{
return;
}
skeletonAnimation.state.SetAnimation (0, name, loop);
currentAnimation = name;
}
void Jump (string animName)
{
asherBlackRB.AddForce (jumpVector, ForceMode2D.Force);
SetAnimation (animName, true);
print ("Doing a jump using the " + animName + " animation");
}
// Physics Updates
void FixedUpdate ()
{
asherBlackRB.velocity = new Vector2 (xDir, asherBlackRB.velocity.y);
}
}
据我所知,在// Jump Button
if块中,您将isGrounded
设置为false,但在此之后,您根据碰撞检测设置isGrounded
:
isGrounded = Physics2D.OverlapCircle (grounderPosition.transform.position, grounderRadius, grounderLayerMask);
当你得到这个语句时,你仍然在处理同一帧,而角色仍然没有时间移动。当你到达更新函数结束时,isGrounded
将为真。在下一次更新时,动画将由第一组if/else块恢复为漫游/空闲动画。您可能希望将冲突检测语句放在跳转if
的else块中。不过,这可能还不够,因为帧之间的时间可能不够长,角色可能无法移动到离地面足够远的地方。在这种情况下,你可能会拿着计数器计数,比如说,5帧,直到你回去检查与地面的碰撞。