x


Making a gun shoot

Hi, I'm new to Unity and I'm trying to make a simple FPS. My problem is that when I shoot, the bullet doesn't go where I'm aiming, it just shoots right in front of me even if I'm aiming all the way up or down. Here's a screenshot. http://img21.imageshack.us/img21/1820/screenshotxwq.png

Here's the script I used for shooting. (And I don't really know much about scripting, I just got this from tutorials and stuff)

var BulletPrefab:Transform;
var force : float = 2000;

function Update()
{
if(Input.GetButtonDown("Fire1"))
{
var bullet = Instantiate(BulletPrefab, 

GameObject.Find("spawnPoint").transform.position, 

GameObject.Find("Gun").transform.rotation);

bullet.rigidbody.AddForce(transform.forward * force);

}
}

How do I fix this?

Thanks.

more ▼

asked Mar 05 '10 at 02:06 AM

Steve 1 gravatar image

Steve 1
1 1 1 2

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

4 answers: sort voted first

It looks like the transform "forward" that you're referring to in that last line is the transform of whatever GameObject this script is attached to, rather than the transform of the bullet or the gun, which is probably what you need:

bullet.rigidbody.AddForce(bullet.transform.forward * force);

Alternatively, you could add a Constant Force Component to the bullet prefab and it could handle its own motion.

more ▼

answered Mar 05 '10 at 06:58 AM

Design3.com gravatar image

Design3.com
640 1 1 10

That worked, thanks!

Mar 06 '10 at 08:26 PM Steve 1

my bar at the bottom keeps on saying, insert a semicolon at the end

Oct 14 '10 at 01:32 AM Joe 8

It simply means you need to insert a semicolon on the line as instructed. If you new to Unity, Please, rather then relying on people here to make the game for you, read a book.

Thank you,

Nov 11 '12 at 05:45 AM 3

Where do I put this script? What I have is: First Person Controller Graphics (don't worry about this) Main Camera M4 (which is the texture of my gun)

I put the script in M4 but it didn't work. Where should I put it?

Dec 05 '12 at 07:00 AM Starman385

it works fantastic thanks

Dec 28 '12 at 11:13 AM Nick Carpeggiani
(comments are locked)
10|3000 characters needed characters left

var BulletPrefab :Transform; var force : float = 2000;

function Update() { if(Input.GetButtonDown("Fire1")) { var bullet = Instantiate(BulletPrefab,

GameObject.Find("spawnPoint").transform.position,

Quaternion.identity);

bullet.rigidbody.AddForce(transform.forward * force);

} }

try this script.make sure that your bullet prefab has a rigidbody attached to it.

more ▼

answered Dec 07 '12 at 02:13 PM

Warren Pope gravatar image

Warren Pope
16

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

if the spawnpoint is at th end of the gun, and if it follows the camera this shouldn't happen.

more ▼

answered Feb 27 '11 at 04:25 PM

John 30 gravatar image

John 30
1

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

I Dont see my bullit fly away can somebody help me please??? :(

more ▼

answered Oct 24 '11 at 06:51 AM

tom1103 gravatar image

tom1103
-11 8 13

Ok, you see what you just did there? That's not an answer, that's another question- and it's not even a good one. Please, don't do that.

Oct 24 '11 at 07:34 AM syclamoth
(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:

x5061
x446
x308

asked: Mar 05 '10 at 02:06 AM

Seen: 17924 times

Last Updated: Dec 28 '12 at 11:13 AM