Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为动画师设置浮点值不工作_C#_Unity3d_Animator - Fatal编程技术网

C# 为动画师设置浮点值不工作

C# 为动画师设置浮点值不工作,c#,unity3d,animator,C#,Unity3d,Animator,函数SetFloat()不识别角色在x轴上移动时的速度,但仍然识别y轴。 我不明白为什么玩家的x速度没有签名到动画师创建的“速度”浮动上 public float acceleration; public bool isGrounded = true; public float jumpHeight; Animator anim; // Use this for initialization void Start () { anim = GetComponent<Animator

函数SetFloat()不识别角色在x轴上移动时的速度,但仍然识别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);
    }

}
公众浮标加速;
公共bool isGrounded=true;
公众浮标高度;
动画师;
//用于初始化
无效开始(){
anim=GetComponent();
}
//每帧调用一次更新
无效更新(){
if(Input.GetKey(KeyCode.Space)和&isground==true){
GetComponent().velocity=新矢量2(0,跳跃高度);
isfounded=false;
}
if(GetComponent().velocity.y==0){
isGrounded=true;
}
anim.SetFloat(“速度”,Mathf.Abs(GetComponent().velocity.x));
if(Input.GetKey(KeyCode.D)){
transform.position+=新矢量3(加速度*时间增量,0.0f,0.0f);
transform.localScale=新向量3(5,5,5);
}
if(Input.GetKey(KeyCode.A)){
transform.localScale=新向量3(-5,5,5);
变换位置-=新矢量3(加速度*时间增量,0.0f,0.0f);
}
}

我不知道这个问题是否仍然相关,但是,如果您想在animator中设置一个浮动,您不能使用transform.position。transform.position不会更改任何刚体值,为此,您可能希望使用Rigidbody.MovePosition或Rigidbody.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));
}
函数更新()
{
float keyboard x=输入.GetAxis(“水平”)*加速度*Time.deltaTime;
刚性。移动位置(键盘X);
anim.SetFloat(“速度”,Mathf.Abs(GetComponent().velocity.x));
}
这应该行得通。再说一次,我不确定这有多重要