x


My Tank Turret Wont Fire?

So i am working on a top-down tank shoot em up and i have the controls working but not the cannon. I think im doing it right but its not fireing when i press my mouse 0 button have a look.

function update()
{
    if(Input.GetKeyDown(KeyCode.Mouse0))
    {
        var bullet = Instantiate(bulletPrefab, transform.Find("cannonShoot").transform.position, Quaternion.identity);
        bullet.rigidbody.AddForce(bullet.transform.forward * 2000);
    }
}

"cannonShoot" is a empty game object in front of my tank's barrel

Also i need a way to rotate to a specific angle over a period of time... currently im just using snap to angles for movements as shown below.

private var z = 0.0;
private var x = 0.0;
private var y = 0.0;
function Update() 
{
var leftButton = Input.GetKeyDown(KeyCode.A);
var upButton = Input.GetKeyDown(KeyCode.W);
var rightButton = Input.GetKeyDown(KeyCode.D);
var downButton = Input.GetKeyDown(KeyCode.S);
var angles = transform.eulerAngles;
//Basic Movements
if(leftButton)
{   
    var rotationLeft = Quaternion.Euler(x, -90, z);
    transform.rotation = rotationLeft;
}
if(upButton)
{   
    var rotationUp = Quaternion.Euler(x, 0, z);
    transform.rotation = rotationUp;
}
if(rightButton)
{
    var rotationRight = Quaternion.Euler(x, 90, z);
    transform.rotation = rotationRight;
}
if(downButton)
{
    var rotationDown = Quaternion.Euler(x, -180, z);
    transform.rotation = rotationDown;
}
//Two buttons at once
if(downButton && leftButton)
{
    var rotationDownLeft = Quaternion.Euler(x, 45, z);
    transform.rotation = rotationDownLeft;
}


}

Any help would be appreciated.

more ▼

asked Aug 17 '10 at 03:42 AM

Raul gravatar image

Raul
1 2 2 2

I wouldn't recommend using Find every time you shoot. If the cannonshoot game object never changes just store a reference to it, it'll be much faster

Aug 17 '10 at 10:31 AM spinaljack
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

i do have an alternative although it it's self has a few problems, if i aim up (as i happen to be making a tank game 2) it still fires forward instead of up, and if i turn the cannon of the tank and fire it will fire the direction of the tank body and not the gun... but it fires :)

//Shooting

var bullitPrefab:Transform;

(i don't know how 2 do the fancy script thing on here) but this at the top with other variables

if(Input.GetButtonDown("Fire1")) { var bullit = Instantiate(bullitPrefab, GameObject.Find("spawn").transform.position, Quaternion.identity); bullit.rigidbody.AddForce(transform.forward * 7000); }

and this somewhere within the moving stuff, (your main script btw) the number above (7000) make it bigger to fire faster or lower to slow.

function OnCollisionEnter(){ Destroy(gameObject);

} function Update () { }

make a new script and fill all of it with the above and but it on the bullet to make it disappear on collision, and make the bullets rigidbody, (remove gravity on bullets if you want them not to fall) with the disappear bit it will still have effects on like other rigidbody objects so its good :) hope this helps.... bloody hell i've written allot. :O

more ▼

answered Nov 21 '10 at 07:39 PM

oinkoinkflapflap gravatar image

oinkoinkflapflap
193 42 54 66

(comments are locked)
10|3000 characters needed characters left

very sorry, forgot 2 add that you will need 2 make an object with no collider or render were u want the bullets to spawn and call it "spawn" (no capitals)

more ▼

answered Nov 22 '10 at 05:13 PM

oinkoinkflapflap gravatar image

oinkoinkflapflap
193 42 54 66

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x329
x172

asked: Aug 17 '10 at 03:42 AM

Seen: 1140 times

Last Updated: Aug 17 '10 at 03:42 AM