简体   繁体   English

Unity2D 碰撞和一些物理

[英]Unity2D collisions and some physics

I'm making a 2D Tank shooter game, but I got some problems and questions:我正在制作一个 2D 坦克射击游戏,但我遇到了一些问题和疑问:

  1. I got some problems with collisions.我遇到了一些碰撞问题。

GIF of a problem here. 这里有问题的GIF。 Go to tank collision problem. 转到坦克碰撞问题。 (I can't post more than 2 links because of low reputation, so You will have to go to images manualy, sorry.) (由于声誉低,我不能发布超过 2 个链接,因此您必须手动转到图像,抱歉。)

I need to make my tank do not do like shown above.我需要让我的坦克不像上图那样做。 I'm using rigidbody on empty parent and box collider on tank body.我在空的父级上使用刚体,在坦克身上使用箱体碰撞器。

My "Tank (root)" in inspector and "tankBody" (hull) in inspector is here.我在检查员中的“Tank(root)”和检查员中的“tankBody”(船体)在这里。

tank movement code:坦克运动代码:

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. My bullets fly like they are in zero gravity/space.我的子弹飞起来就像在零重力/空间中一样。 I need them to do not hover like that(I got similar problem before and I couldn't fixed it..).我需要它们不要像那样悬停(我以前遇到过类似的问题,但无法修复它..)。 There is gif in first link in 1.st problem.第一个问题的第一个链接中有 gif。 shooting code:拍摄代码:

    using UnityEngine;使用 UnityEngine;

    using System.Collections;使用 System.Collections;

    public class Shooting : MonoBehaviour {公开课射击: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); } }

    } }

I managed to fix my both problems.我设法解决了我的两个问题。 To fix problem number 1. I used add force.解决问题 1。我使用了 add force。 My new moving forwand and backward looks like this:我新的前后移动看起来像这样:

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);

and I had to adjust my mass to a smaller (from 2000 to 1), thrust to a bigger (from 0.2 to 50000) and set drag into 50, angular drag 100.我不得不将质量调整为更小(从 2000 到 1),将推力调整为更大(从 0.2 到 50000)并将阻力设置为 50,角阻力设置为 100。

2nd problem got fixed by setting drag and angular drag into a bigger value.通过将阻力和角度阻力设置为更大的值,解决了第二个问题。 That's it!就是这样!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM