I'm making a 2D Tank shooter game, but I got some problems and questions:
- 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);
}
}
}
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); } }
}