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