在Unity c#中翻转精灵之前播放2D动画#

时间:2017-08-27 19:10:52

标签: c# animation sprite delay unity2d

我实际上能够与敌人做到这一点,但出于某种原因,如果它是玩家,我无法让它发挥作用。

请参阅,播放器处于默认的空闲动画中。当我从相反的方向按下箭头键时(默认是游戏开始时的右边 - >),我希望它在精灵翻转其x比例之前播放转动动画。

然而,当我按下箭头键转动他时,它现在正在做什么,首先,它会快速翻转精灵,然后执行动画,好像它还没有翻过来一样,然后再次翻转到另一个方向。

在我的动画师中,空闲没有退出节点的退出时间,而翻转节点确实有退出时间回到空闲状态,以防您查询。我已经尝试过在这里和那里调用一个计时器,但到目前为止还没有运气。有人可以帮忙吗?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class tulMoveMount : MonoBehaviour {

    private Animator anim;
    private Rigidbody2D rb;
    public Image manaBar;

    public LayerMask whatIsGround;
    private bool grounded = false;
    public Transform groundCheck;
    public float groundCheckRadius;

    private bool goRight = true;
    private bool jump;
    private bool turn = false;
    private bool idle = true;
    private bool mountOff;
    private bool turnComplete = false;  

    public float runSpeed;
    public float walkSpeed; 
    private float move; 
    public float turnDelay = 2.25f;     
    public float timer3 = 2.26f;

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

    void Update () 
    {
        grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
        timer -= Time.deltaTime;
        turnDelay -= Time.deltaTime;
        HandleMovement ();

    }

    void HandleMovement()
    {

        float move = Input.GetAxis ("Horizontal");
        float moveV = Input.GetAxis ("Vertical");

        {
            rb.velocity = new Vector2 (move * walkSpeed, rb.velocity.y);

            anim.SetFloat ("walkSpeed", Mathf.Abs (move));

        }

        if (!goRight && move > 0) {
            FlipConditions ();
            Invoke ("ResetValues",timer3 );
            Flip ();
            turnComplete = false;
        }
        if (goRight && move < 0) {
            FlipConditions ();
            Invoke ("ResetValues",timer3 );
            Flip ();
        }
    }

    void FlipConditions()//
    {
        idle = false;
        turn = true;
        anim.SetTrigger ("turn");
        idle = true;
        anim.SetTrigger ("idle");
    }

    void Flip()
    {
        goRight = !goRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
        turnComplete = false;
    }
    void ResetValues()
    {
        idle = true;
        anim.SetTrigger ("idle");
    }
}

1 个答案:

答案 0 :(得分:0)

LateUpdate()执行任何动画后,您可以尝试在Update()中翻转精灵。尝试类似的东西,将动画放在更新:

// store references to components on the gameObject
Transform _transform;
Rigidbody2D _rigidbody;

// hold player motion in this timestep
float vx;
float vy;

float MoveSpeed;

void Awake () {

    // get a reference to the components we are going to be changing and store a reference for efficiency purposes
    transform = GetComponent<Transform> ();

    rigidbody = GetComponent<Rigidbody2D> ();

}

// this is where most of the player controller magic happens each game event loop
void Update()
{
    // determine horizontal velocity change based on the horizontal input
    vx = Input.GetAxisRaw ("Horizontal");

    // get the current vertical velocity from the rigidbody component
    vy = rigidbody.velocity.y;

    // Change the actual velocity on the rigidbody
    rigidbody.velocity = new Vector2(vx * MoveSpeed, vy);
}

// Checking to see if the sprite should be flipped
// this is done in LateUpdate since the Animator may override the localScale
// this code will flip the player even if the animator is controlling scale
void LateUpdate()
{
    // get the current scale
    Vector3 localScale = transform.localScale;

    if (vx > 0) // moving right so face right
    {
        facingRight = true;
    } else if (vx < 0) { // moving left so face left
        facingRight = false;
    }

    // check to see if scale x is right for the player
    // if not, multiple by -1 which is an easy way to flip a sprite
    if (((facingRight) && (localScale.x<0)) || ((!facingRight) && (localScale.x>0))) {
        localScale.x *= -1;
    }

    // update the scale
    transform.localScale = localScale;
}