gpt4 book ai didi

c# - GetKeyDown 输入仅播放第一帧动画

转载 作者:行者123 更新时间:2023-11-30 23:31:38 25 4
gpt4 key购买 nike

我正在尝试控制一组角色动画,但我在处理跳跃动画时遇到了问题。我的水平行走动画工作正常,但是当我按下跳跃按钮时,我希望播放两个跳跃动画之一,具体取决于玩家是处于空闲状态还是行走状态。但是,当我当前按下跳跃按钮时,它只播放相应跳跃动画的第一帧,然后立即返回空闲或行走状态。如何在按下跳跃按钮时播放整个跳跃动画?

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;
float yDir;
string currentAnimation = "";

void Start ()
{
// Create a reference to Asher Black Rigidbody2D
asherBlackRB = GetComponent<Rigidbody2D>();
}

void Update ()
{
xDir = Input.GetAxis ("Horizontal") * moveSpeed;

if (xDir > 0) // ------ Walk Right
{
goalRotation = Quaternion.Euler (0, 0, 0);
SetAnimation ("Walk", true);
}
else if (xDir < 0) // ------ 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;

if (xDir > 0)
{
Jump ("Walk-Jump");
}
else if (xDir < 0)
{
Jump ("Walk-Jump");
}
else
{
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 animationName)
{
asherBlackRB.AddForce (jumpVector, ForceMode2D.Force);
SetAnimation (animationName, true);
}

// Physics Updates
void FixedUpdate ()
{
asherBlackRB.velocity = new Vector2 (xDir, asherBlackRB.velocity.y);
}
}

最佳答案

您的跳跃动画无法正确显示,因为您总是在下一帧将其改回另一个动画。例如,空转:

else
{
SetAnimation ("Idle", true);

if (Input.GetKeyDown("space"))
{
SetAnimation ("Idle-Jump", true);
}
}

角色空闲的每一帧,无论角色是否跳跃,都会设置空闲动画。但是,只要按住空格键,就会切换回跳跃动画的第一帧。

您可能应该有一个 bool 值来指示角色何时跳跃。每次将角色设置为跳跃动画时,您都将其设置为 true。然后你可以调整你的 if 语句,让它首先处理的是处于跳跃状态的角色,这样你的跳跃逻辑就可以在没有任何干扰的情况下完成。当角色完成跳跃后,将 bool 值设置回 false。所以,像这样:

bool jumping = false;

// User Input
void Update ()
{
xDir = Input.GetAxis ("Horizontal") * moveSpeed;

if (jumping)
{
// Process jumping here
if (/* Check for jumping finished here */)
{
// Finished jumping
jumping = false;
}
}
else if (xDir > 0)
{
goalRotation = Quaternion.Euler (0, 0, 0);
SetAnimation ("Walk", true);

if (Input.GetKeyDown("space"))
{
SetAnimation ("Walk-Jump", true);
jumping = true;
}
}
else if (xDir < 0)
{
goalRotation = Quaternion.Euler (0, 180, 0);
SetAnimation ("Walk", true);

if (Input.GetKeyDown("space"))
{
SetAnimation ("Walk-Jump", true);
jumping = true;
}
}
else
{
SetAnimation ("Idle", true);

if (Input.GetKeyDown("space"))
{
SetAnimation ("Idle-Jump", true);
jumping = true;
}
}

graphics.localRotation = Quaternion.Lerp (graphics.localRotation, goalRotation, rotateSpeed * Time.deltaTime);
}

关于c# - GetKeyDown 输入仅播放第一帧动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34534281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com