【问题标题】:Unity2D collisions and some physicsUnity2D 碰撞和一些物理
【发布时间】:2015-09-19 17:08:30
【问题描述】:

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

  1. 我遇到了一些碰撞问题。

GIF of a problem here. Go to tank collision problem.(由于声誉低,我不能发布超过 2 个链接,所以您必须手动访问图片,抱歉。)

我需要让我的坦克不要像上面显示的那样。我在空父对象上使用刚体,在坦克体上使用盒子碰撞器。

inspector 中的“Tank (root)”和inspector 中的“tankBody”(hull) 是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. 我的子弹像在零重力/空间中一样飞行。我需要他们不要那样悬停(我之前遇到过类似的问题,但我无法修复它..)。第一个问题的第一个链接中有 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);
        }
    }
    

    }

【问题讨论】:

  • 只是一个旁注:使用平移来移动启用了刚体的对象并不是最好的主意。您应该像使用火箭一样在坦克上使用 addforce 或直接设置速度。您可能还想为刚体添加插值。
  • @PockeTiger 是的,我知道刚体上的变换不是最好的使用方法,但我不知道如何在没有它的情况下移动它们。我尝试了 Rigidbody2D.MovePosition 但它没有帮助而且我对力量的理解不够好。你能帮忙吗?

标签: c# unity3d 2d game-physics collision


【解决方案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。

通过将阻力和角度阻力设置为更大的值来解决第二个问题。就是这样!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多