gpt4 book ai didi

c# - 如何在动画播放时延迟统一运动

转载 作者:太空宇宙 更新时间:2023-11-03 20:49:05 24 4
gpt4 key购买 nike

我在 Unity 2018.4 中很难延迟我的角色的移动,我怀疑这更多的只是我的代码的问题而不是 unity。

编辑:我希望能够按住右箭头键,让角色不动 415 毫秒,然后能够移动 580 毫秒,之后他不能移动 350 毫秒(让动画完成播放)

我尝试在 IE 分子中使用倒计时等待然后调用我的移动函数,但这导致我的角色在动画播放后没有移动。

//What makes the character move, works fine if executed on its own in update
private void ExecuteMovement(float dir)
{
currentPosition = transform.position;
currentPosition.x += dir * Time.deltaTime * speed;
transform.position = currentPosition;
}

//Trying to use waitforseconds to delay the execution while the animation plays
private IEnumerator Movement(float dir)
{
yield return new WaitForSeconds(0.5f);

ExecuteMovement(dir);
}

void Update()
{
if (0 > Input.GetAxis("Horizontal"))
{
//This is to flip image
transform.eulerAngles = new Vector3(0, 180, 0);

//starts animation
animator.SetBool("isSkipping", true);

//calls the movement function with the direction to move in
Movement(Input.GetAxis("Horizontal"));

}

else if (0 < Input.GetAxis("Horizontal"))
{
//This is to flip image
transform.eulerAngles = new Vector3(0, 0, 0);

//starts animation
animator.SetBool("isSkipping", true);

//calls the movement function with the direction to move in
Movement(Input.GetAxis("Horizontal"));

}
}

我很乐意尝试任何其他方法来延迟角色的移动。动画显示角色冲上去跳跃,然后跳跃。我只想在它在空中时移动,这大约是动画的半秒。

最佳答案

您正在尝试像调用方法一样调用您的 IEnumerator


相反,您必须将其作为 Coroutine 启动。使用 StartCoroutine

StartCoroutine(Movement(Input.GetAxis("Horizontal")));

看到你的编辑

I want to be able to hold down the right arrow key, have the character not move for 415ms, then be able to move for 580ms, after which he cannot move for 350ms (to let the animation finish playing)

到现在为止,既然您想要继续移动但仍然完全控制速度,我会将其保留在 Update 中并仅使用协程来控制标志。协程仍然更易于读/写和维护。 (现在它也允许空中方向切换..不知道你是否想要这个):

// control the ability to move with a simple flag
// instead of various states
public bool canMove;

// check if a jumping is already being animated/executed
// -> disallow a new jumping animation until it is finished
private bool canJump = true;

private void Update()
{
// avoid repeated API calls
var input = Input.GetAxis("Horizontal");

if (input < 0)
{
//This is to flip image
transform.eulerAngles = new Vector3(0, 180, 0);

if(canJump)
{
//starts the movement
StartCoroutine(Jump());
}
}
else if (input > 0)
{
//This is to flip image
transform.eulerAngles = new Vector3(0, 0, 0);

if(canJump)
{
//starts the movement
StartCoroutine(Jump());
}
}

// if can not move do nothing else
if(!canMove) return;

// execute the movement
transform.position += Vector3.right * input * speed * Time.deltaTime;
}

private IEnumerator Jump()
{
// disable jumping
canJump = false;

// disable move during animation
canMove = false;

//starts animation
animator.SetBool("isSkipping", true);

// not move for 415ms
yield return new WaitForSeconds(0.415f);

// enable move
canMove = true;

// be able to move for 580ms
yield return new WaitForSeconds(0.580f);

// disable move
canMove = false;

// cannot move for 350ms during end of animation
yield return new WaitForSeconds(0.350f);

// enable move again
canMove = true;

// re-enable jumping
canJump = true;
}

关于c# - 如何在动画播放时延迟统一运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57299904/

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