2015-05-10 23 views
0

函數SetFloat()無法識別我的角色在x軸中移動時的速度,但仍識別y軸。 我不明白爲什麼player'sx速度沒有簽署在動畫創作的「速度」浮動動畫師無法使用的SetFloat

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); 
    } 

} 

回答

0

我不知道,如果這個問題仍然是相關的,但是,如果你想設置動畫師中的浮動元素,你不能使用transform.position。 transform.position不會更改任何Rigidbody值,爲此,您需要使用rigidbody.MovePosition或rigidbody.AddForce移動Rigidbody。

我建議使用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)); 
} 

這應該工作。再次,我不確定這是多麼相關。