2015-07-02 48 views
2

我正在做一個2D坦克射擊遊戲,但我得到了一些問題和疑問:Unity2D碰撞和一些物理

  1. 我有一些問題與衝突。

GIF of a problem here. Go to tank collision problem.(我不能發佈,因爲低信譽的超過2個鏈接,所以你必須要到的圖像manualy,對不起。)

我需要讓我的油箱不要做像顯示以上。我在坦克車身上的空父母和箱子對撞機上使用剛體。

我的 「罐(根)」,在檢查和 「tankBody」(船體)在檢查員是here.

罐運動代碼:

using UnityEngine; 
using System.Collections; 

public class Movement : MonoBehaviour { 
    public float thrust; 
    public float rotatingspeed; 
    public Rigidbody rb; 

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

void Update() { 
    if (Input.GetKey (KeyCode.W)) { 
     transform.Translate (Vector2.right * thrust);   
    } 
    if (Input.GetKey (KeyCode.S)) { 
     transform.Translate (Vector2.right * -thrust); 
    } 
    if(Input.GetKey(KeyCode.A)) { 
     transform.Rotate(Vector3.forward, rotatingspeed); 
    } 
    if(Input.GetKey(KeyCode.D)) { 
     transform.Rotate(Vector3.forward, -rotatingspeed); 
    } 

} 

}

  • 我的子彈像零重力/空間一樣飛行。我需要他們不要像那樣懸停(我之前得到類似的問題,我無法修復它)。 1.st問題中的第一個環節中有gif。 拍攝編號:

    使用UnityEngine;

    using System.Collections;

    公共類射擊:MonoBehaviour {

    public Rigidbody2D projectile; 
        public float speed = 20; 
        public Transform barrelend; 
    
    void Update() { 
        if (Input.GetButtonDown("Fire1")) 
        { 
         Rigidbody2D rocketInstance; 
         rocketInstance = Instantiate(projectile, barrelend.position, barrelend.rotation) as Rigidbody2D; 
         rocketInstance.AddForce(barrelend.right * speed); 
        } 
    } 
    

    }

  • +0

    只是一個註釋:使用translate來移動剛體啓用對象不是最好的主意。你應該像你的火箭一樣在坦克上使用附加力,或者直接設置速度。您可能還想要爲剛體添加插值。 – PockeTiger

    +0

    @PockeTiger是的,我知道剛體上的變換不是最好的選擇,但我不知道如何在沒有它的情況下移動它們。我嘗試了Rigidbody2D.MovePosition,但它沒有幫助,我不明白部隊是否足夠好。你能幫我嗎? – Justasmig

    回答

    1

    我設法解決我的這兩個問題。 修復問題編號1.我使用了加力。我到新的移動forwand落後看起來像這樣:

    if (Input.GetKey (MoveForward)) { 
         //transform.Translate (Vector2.right * thrust); OLD !! 
         rb2D.AddForce(transform.right * thrust * Time.deltaTime); 
        } 
    if (Input.GetKey (MoveBackward)) { 
         //transform.Translate (Vector2.right * -thrust); OLD !! 
         rb2D.AddForce(transform.right * -thrust * Time.deltaTime); 
    

    ,我有我的質量調整到一個較小的(從2000年到1),推力更大(0.2〜50000),並設置拖動到50,角度阻力100.

    第二個問題已通過將拖動和角度拖動設置爲更大的值得到解決。而已!