x


Shooting Script Bullet Doesnt Fire

I had a working script, then somewhere along the line something got deleted and I cannot figure out how to get back to where is was. I have a driveable tank with a game object attached to the end of the barrel that fires projectiles on a left mouse click. With the following script however, my bullets spawn but dont move. What have I miscalculated?

using UnityEngine;
using System.Collections;

public class fireBullet : MonoBehaviour {

    public Rigidbody bullet;
    public float cannonPower;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
       //fire cannon
       if(Input.GetButtonUp("Fire1")){

       //create projectile at location script is attached to
         Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;

       //add force to projectile
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
         instance.AddForce(fwd*cannonPower);

       }
    }
}
more ▼

asked Sep 11 '12 at 07:12 PM

xJames720x gravatar image

xJames720x
41 3 6 9

Is it possible that the bullets are colliding with the tank before they start moving, and that's stunting them back into being stationary? EDIT - See Neurological's answer to see what I mean.

Sep 11 '12 at 09:03 PM mtrc
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Maybe it get stuck with the collider, try adding this:

Physics.IgnoreCollision(bullet.collider, transform.root.collider);

or

Physics.IgnoreCollision(instance.collider, transform.root.collider);

more ▼

answered Sep 11 '12 at 09:02 PM

Neurological gravatar image

Neurological
303 4 10 15

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

You wrote GetButtonUp, but you may want try to replace it by GetButton or GetButtonDown.

more ▼

answered Sep 11 '12 at 09:29 PM

NhommeBeurreOne gravatar image

NhommeBeurreOne
90 7 10 12

(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:

x5276
x320
x298
x261

asked: Sep 11 '12 at 07:12 PM

Seen: 352 times

Last Updated: Sep 11 '12 at 09:29 PM