【问题标题】:Animation gets interrupted, but I want the animation to finish动画被打断,但我希望动画完成
【发布时间】:2019-01-31 12:50:57
【问题描述】:

我是编码新手,我一直在玩 Unity,但遇到了问题。

我的问题是,如果我移动播放器并激活功能SkillAttack1,动画将取消并且无法完成。现在,正如您在下面的代码中看到的if I activate theSkillAttack1`,它可以工作,但仍然无法完成动画。另外,例如,如果我按下“w”按钮 15 秒,玩家就会卡住并向前移动。播放完整动画的唯一方法是不动,但这是不对的。

[Animator1][1]
[Animator2][2]
[Animator3][3]

[1]: https://i.stack.imgur.com/BeziY.png
[2]: https://i.stack.imgur.com/310vz.png
[3]: https://i.stack.imgur.com/tOVVu.png






using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{   

public float velocity = 5;
public float turnSpeed = 10;
Vector2 input;
float angle;
Quaternion targetRotation;
public Transform cam;
public Animator anim;
public bool IsAttacking = false;


void Start()
{
    anim = GetComponent<Animator>();
}

void Update()
{
    if(Mathf.Abs(input.x) < 0.5 && Mathf.Abs(input.y) < 0.5) return;
    CalculateDirection();
    Rotate();
    Move(); 
}

void FixedUpdate()
{
    if(IsAttacking == false)
    {
        GetInput();
    }
    SkillAttack1();
    SkillAttack2();
}

/// Input based on Horizontal(a,d) and Vertical (w,s) keys
void GetInput()
{
        input.x = Input.GetAxis("Horizontal");
        input.y = Input.GetAxis("Vertical");
        anim.SetFloat("VelX", input.x);
        anim.SetFloat("VelY", input.y);
}


/// Direction relative to the camera rotation
void CalculateDirection()
{
    angle = Mathf.Atan2(input.x, input.y);
    angle = Mathf.Rad2Deg * angle;
    angle += cam.eulerAngles.y; 
}

/// Rotate toward the calculated angle
void Rotate()
{
    targetRotation = Quaternion.Euler(0, angle, 0);
    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed 
* Time.deltaTime);
}

/// This player only move along its own forward axis
void Move()
{
    transform.position += transform.forward * velocity * Time.deltaTime;
}

public void SkillAttack1(){
    if(Input.GetKeyDown("1"))
    { 
        IsAttacking = true;
        StartCoroutine(OnSkillAttack1());
    }
}

public void SkillAttack2()
{
    if(Input.GetKeyDown("2"))
    { 
        anim.SetTrigger("SkillAttack2");
    }
}


public IEnumerator OnSkillAttack1()
{
    anim.SetTrigger("SkillAttack1");
    yield return null;

    yield return new WaitForSeconds(15.0f);
    IsAttacking = false;
}
}

【问题讨论】:

标签: c# unity3d


【解决方案1】:

看起来您的动画控制器正在被您的移动代码覆盖,当您的播放器发送一些输入以向左或向右行走时,它会在您的动画控制器上设置 VelX 和 VelY 变量。如果无法访问您的控制器,我无法确切告诉您为什么它会中断攻击,但很可能您设置了一个中断,当其中一个变量高于 0 并转换为“行走”动画时,它会从“任何状态”触发你的控制器有。

您需要调整动画过渡以处理这种潜在情况,或者防止在攻击动画期间触发移动代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    • 2011-05-04
    • 2015-07-17
    • 2017-07-08
    • 1970-01-01
    • 2020-05-21
    相关资源
    最近更新 更多