2016-10-06 15 views
1
using UnityEngine; 
using System.Collections; 

public class Accelerometer : MonoBehaviour { 

public Vector3 positionplayer; 

// Update is called once per frame 
void Update() 
{ 
    transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z); 
    transform.Rotate (0,0,0); 
} 

} 

我重視這個腳本的球員,每一件事工作正常,但是當它撞到障礙物啓動bouncing..but它不「T使用利用立方體當玩家在牆上它開始反彈擊中,如何讓正確的球員招

對於播放器平面和四個壁已經添加了對撞機,剛體和剛體設置爲與質量重力上我已接地的接地(只有當壁被擊中)

反彈1

爲牆壁我添加了箱子collider和j UST一個剛體

這在我的新代碼

using UnityEngine; 
using System.Collections; 

public class Accelerometer : MonoBehaviour { 

public Vector3 positionplayer; 
Rigidbody b; 
public float speed = 10.0f; 
private Quaternion localRotation; 

// Use this for initialization 
void Start() { 

localRotation = transform.rotation; 

} 

// Update is called once per frame 
void Update() 
{ 

float curSpeed = Time.deltaTime * speed; 

//transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z); 

transform.position += (transform.right * Input.acceleration.x) * curSpeed; 

transform.position += (transform.forward * -Input.acceleration.z) * curSpeed; 

//transform.Rotate (Input.acceleration.x,0,0); 



localRotation.z += -Input.acceleration.z * curSpeed; 
localRotation.z += Input.acceleration.z * curSpeed; 

//transform.rotation = localRotation; 

print (transform.position); 
} 
} 
+0

這可能不是有關您的情況,但如果你不想讓你的變換旋轉,你應該在剛體檢查設置,使那些值不會改變。它用x/y/z座標分隔它,所以你可以對它有特異性。 –

回答

1

這裏有一些東西,你可以嘗試:

  • 從牆壁
  • 刪除rigidbody使用rigidBody組件,而不是transform.Translate移動您的播放器。

下面是一個Unity Tutorial示例代碼:

using UnityEngine; 
using System.Collections; 

public class PlayerController : MonoBehaviour 
{ 
    public float speed; 
    Rigidbody rigidbody; 

    void Start() 
    { 
     rigidbody = GetComponent<Rigidbody>(); 
    } 

    void FixedUpdate() 
    { 
     float moveHorizontal = Input.GetAxis ("Horizontal"); 
     float moveVertical = Input.GetAxis ("Vertical"); 
     Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); 
     rigidbody.velocity = movement * speed; 
    } 
} 
+0

當我將這個腳本添加到播放器中時,播放器根本沒有移動 –

+0

@Umair_M我有一個新的腳本在我的que中給出...它實際上除了旋轉,爲什麼旋轉不起作用 –

+0

您正在使用Quaternion。它的w,x,y,z分量不能用作傳統的x,y,z旋轉。改用EulerAngles。就像我的回答一樣。 –

相關問題