【问题标题】:SetFloat for animator not working动画师的 SetFloat 不起作用
【发布时间】:2015-07-21 02:38:14
【问题描述】:

当我的角色沿 x 轴移动时,函数 SetFloat() 无法识别速度,但仍能识别 y 轴。 我不明白为什么玩家的 x 速度没有签名到动画师中创建的“速度”浮动

public float acceleration;
public bool isGrounded = true;
public float jumpHeight;
Animator anim;

// Use this for initialization
void Start () {
    anim = GetComponent<Animator>();
}

// Update is called once per frame
void Update () {
    if(Input.GetKey(KeyCode.Space) && isGrounded == true){
        GetComponent<Rigidbody2D>().velocity = new Vector2 (0 , jumpHeight);
        isGrounded = false;
    }

    if(GetComponent<Rigidbody2D>().velocity.y == 0){
        isGrounded = true;
    }

    anim.SetFloat("Speed", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));

    if(Input.GetKey(KeyCode.D)){
        transform.position += new Vector3 (acceleration * Time.deltaTime , 0.0f, 0.0f);
        transform.localScale = new Vector3 (5,5,5);
    }
    if(Input.GetKey (KeyCode.A)){
        transform.localScale = new Vector3 (-5,5,5);
        transform.position -= new Vector3 (acceleration * Time.deltaTime , 0.0f , 0.0f);
    }

}

【问题讨论】:

    标签: c# unity3d animator


    【解决方案1】:

    我不知道这个问题是否仍然相关,但是,如果你想在动画师中设置一个浮点数,你不能使用 transform.position。 transform.position 不会更改任何刚体值,为此您需要使用刚体.MovePosition 或刚体.AddForce 移动刚体。

    我建议使用rigidbody.MovePosition,因为您不必编写太多代码。

    我这样做的方式是我不使用 Input.GetKey,因为它很草率而且效果不如 Input.GetAxis:

    function Update()
    {
    float keyboardX = Input.GetAxis("Horizontal") * acceleration * Time.deltaTime;
    
    rigid.MovePosition(keyboardX);
    
    anim.SetFloat("Speed", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));
    }
    

    这应该可行。同样,我不确定这有多相关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 2015-11-02
      • 1970-01-01
      相关资源
      最近更新 更多