- Home /
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.
Answer by Design3.com · Mar 05, 2010 at 06:58 AM
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.
my bar at the bottom keeps on saying, insert a semicolon at the end
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,
Where do I put this script? What I have is: First Person Controller Graphics (don't worry about this) $$anonymous$$ain Camera $$anonymous$$4 (which is the texture of my gun)
I put the script in $$anonymous$$4 but it didn't work. Where should I put it?
Answer by John 30 · Feb 27, 2011 at 04:25 PM
if the spawnpoint is at th end of the gun, and if it follows the camera this shouldn't happen.
Answer by Warren Pope · Dec 07, 2012 at 02:13 PM
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.