2014-01-21 52 views
0

我有一款2D遊戲,當設備傾斜時需要將玩家rigidbody2D移動到x軸。我下面的代碼是我認爲應該可以工作的代碼,但事實並非如此。玩家根本不動。我在做什麼錯,爲什麼?Android2D上的Unity2D rigidbody2D.velocity

Vector3 dir = Vector3.zero; 
     dir.x = Input.acceleration.y; 
     if (dir.sqrMagnitude > 1) 
      dir.Normalize(); 

     dir *= Time.deltaTime; 
     anim.SetFloat("Speed", Mathf.Abs(dir.x)); 
     rigidbody2D.velocity = new Vector2 (dir.x * tiltSpeed, rigidbody2D.velocity.y); 

下面

using UnityEngine; 
using System.Collections; 

public class CharacterControllerScript : MonoBehaviour { 

public float maxSpeed = 10f; 
bool facingRight = true; 
public float tiltSpeed = 10f; 

Animator anim; 

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

// Update is called once per frame 
void FixedUpdate() { 
    // Set animation 
    if ((Application.platform != RuntimePlatform.Android) || (Application.platform != RuntimePlatform.IPhonePlayer)){ 
     float move = Input.GetAxis ("Horizontal"); 
     anim.SetFloat("Speed", Mathf.Abs(move)); 
     rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y); 
     // Decide what way animation moving 
     if (move > 0 && !facingRight) 
      Flip(); 
     else if (move < 0 && facingRight) 
      Flip(); 
    } 
    else { 
     Vector3 dir = Vector3.zero; 
     dir.x = Input.acceleration.y; 
     if (dir.sqrMagnitude > 1) 
      dir.Normalize(); 

     dir *= Time.deltaTime; 
     anim.SetFloat("Speed", Mathf.Abs(dir.x)); 
     rigidbody2D.velocity = new Vector2 (dir.x * tiltSpeed, rigidbody2D.velocity.y); 
     // Decide what way animation moving 
     if (move > 0 && !facingRight) 
      Flip(); 
     else if (move < 0 && facingRight) 
      Flip(); 
    } 
} 

void Flip() { 
    facingRight = !facingRight; 
    Vector3 thescale = transform.localScale; 
    thescale.x *= -1; 
    transform.localScale = thescale; 
} 
} 
+0

相同的代碼,同樣的問題,我實在想不出我什麼做錯了。 – PaoloCargnin

回答

0

全部的代碼,我希望這個答案仍然是對您有用,因爲你幾月前

發佈我覺得你的代碼將總是通過第一,如果情況。你應該

(Application.platform != RuntimePlatform.Android) && (Application.platform != RuntimePlatform.IPhonePlayer) 

更換

(Application.platform != RuntimePlatform.Android) || (Application.platform != RuntimePlatform.IPhonePlayer) 

由於他們不能同時在同一時間是真實的