C# 统一精灵不切换

C# 统一精灵不切换,c#,unity3d,animation,C#,Unity3d,Animation,我正在Unity中创建一个2D platformer游戏,我现在正在处理精灵动画,这就是问题的根源。 我为精灵切换做了一个跳跃命令和一个下降命令,当玩家落地时,精灵应该会恢复到理想状态,但它会卡在下降的精灵上,而不是一个动画,它看起来更像是一个代码问题。代码如下: using System.Collections.Generic; using System.Collections.Specialized; using UnityEngine; public class PlayerContro

我正在Unity中创建一个2D platformer游戏,我现在正在处理精灵动画,这就是问题的根源。 我为精灵切换做了一个跳跃命令和一个下降命令,当玩家落地时,精灵应该会恢复到理想状态,但它会卡在下降的精灵上,而不是一个动画,它看起来更像是一个代码问题。代码如下:

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

public class PlayerController : MonoBehaviour
{
    private Rigidbody2D rb;
    private Animator anim;
    private Collider2D call;
    [SerializeField] private LayerMask ground;
    private enum State {idle, running, jumping, falling}
    private State state = State.idle; 

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        call = GetComponent<Collider2D>();
    }
    private void Update()
    {
     
        
        float HDirection = Input.GetAxis("Horizontal");
        
        if (HDirection < 0)
        {
            rb.velocity = new Vector2(-5, rb.velocity.y);
            gameObject.GetComponent<SpriteRenderer>().flipX = true;
        }

        else if (HDirection > 0)
        {
            rb.velocity = new Vector2(5, rb.velocity.y);
            gameObject.GetComponent<SpriteRenderer>().flipX = false;
        }

        if(Input.GetButtonDown("Jump") && call.IsTouchingLayers())
        {
            rb.velocity = new Vector2(rb.velocity.x, 10f);
            state = State.jumping;

        }

        velocitySwitch();
        anim.SetInteger("New Int", (int)state);

    }

    private void velocitySwitch()
    {
        if (state == State.jumping)
        {
            if (rb.velocity.y < .1f)
            {
                state = State.falling;
            }
        }
        else if(state == State.falling)
        {
            if (call.IsTouchingLayers(ground))
            {
                state = State.idle;
            }
        }
        else if (Mathf.Abs(rb.velocity.x) > 2f)
        {
            //moving
            state = State.running;
        }
        else
        {
            state = State.idle;
        }
    }
}
使用System.Collections.Generic;
使用System.Collections.Specialized;
使用UnityEngine;
公共类玩家控制器:单行为
{
私有刚体2d rb;
私人动画师;
私人通话;
[序列化字段]专用层掩码地面;
私有枚举状态{空闲、运行、跳跃、下降}
私有状态=State.idle;
私有void Start()
{
rb=GetComponent();
anim=GetComponent();
call=GetComponent();
}
私有void更新()
{
float hddirection=Input.GetAxis(“水平”);
如果(HDirection<0)
{
rb.velocity=新矢量2(-5,rb.velocity.y);
gameObject.GetComponent().flipX=true;
}
否则如果(hddirection>0)
{
rb.velocity=新矢量2(5,rb.velocity.y);
gameObject.GetComponent().flipX=false;
}
if(Input.GetButtonDown(“Jump”)&调用.IsTouchingLayers()
{
rb.velocity=新矢量2(rb.velocity.x,10f);
state=state.jumping;
}
速度开关();
anim.SetInteger(“新整数”,“整数”状态);
}
专用无效速度开关()
{
if(state==state.jumping)
{
if(径向速度y<.1f)
{
state=state.falling;
}
}
else if(state==state.falling)
{
if(调用IsTouchingLayers(地面))
{
state=state.idle;
}
}
如果(平均绝对速度(rb.velocity.x)>2f)则为else
{
//移动的
state=state.running;
}
其他的
{
state=state.idle;
}
}
}

只要
动画师
在某个属性上的任何动画中仅保留一个关键帧,则该属性完全由
动画师“拥有”
。。。因为它就像是在渲染帧之前最后更新的东西之一,所以基本上不可能通过代码来推翻它。。。但总的来说:我看不出您试图在代码中更改精灵的位置/方式。。