2
votes

I'm making a 2D Tank shooter game, but I got some problems and questions:

  1. I got some problems with collisions.

GIF of a problem here. 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.)

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 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. shooting code:

    using UnityEngine;

    using System.Collections;

    public class Shooting : 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
Just a sidenote: It's not the best idea to move a rigidbody enabled object with translate. You should use addforce on your tank as with your rocket or set the velocity directly. You may also want to add interpolation to your rigidbody.PockeTiger
@PockeTiger Yeah I know that transform on rigidbodies is not best thing to use, but I don't can't find out how to move them without it. I tried Rigidbody2D.MovePosition but it didnt help and I don't understand forces good enough. Can you help?Justasmig

1 Answers

2
votes

I managed to fix my both problems. To fix problem number 1. I used 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.

2nd problem got fixed by setting drag and angular drag into a bigger value. That's it!