我正在做一個2D坦克射擊遊戲,但我得到了一些問題和疑問:Unity2D碰撞和一些物理
- 我有一些問題與衝突。
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); } }
}
只是一個註釋:使用translate來移動剛體啓用對象不是最好的主意。你應該像你的火箭一樣在坦克上使用附加力,或者直接設置速度。您可能還想要爲剛體添加插值。 – PockeTiger
@PockeTiger是的,我知道剛體上的變換不是最好的選擇,但我不知道如何在沒有它的情況下移動它們。我嘗試了Rigidbody2D.MovePosition,但它沒有幫助,我不明白部隊是否足夠好。你能幫我嗎? – Justasmig