首页 文章

Unity2D碰撞和一些物理

提问于
浏览
2

我正在制作2D坦克射击游戏,但我遇到了一些问题和疑问:

  • 我遇到了一些碰撞问题 .

GIF of a problem here. Go to tank collision problem.(我不能发布超过2个链接,因为声誉很低,所以你必须手动去图片,对不起 . )

我需要让我的坦克不要像上面所示那样做 . 我在坦克车身的空父母和箱子对撞机上使用刚体 .

检查员中的"Tank (root)"和检查员中的"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;

使用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);
    }
}

}

1 回答

  • 1

    我设法解决了我的两个问题 . 解决问题编号1.我使用了添加力 . 我的新移动转发和向后看起来像这样:

    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 .

    通过将拖动和角度拖动设置为更大的值来解决第2个问题 . 而已!

相关问题