2015-07-02 51 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.

    第二个问题已通过将拖动和角度拖动设置为更大的值得到解决。而已!