- Home /
Bullets won't fire?
I'm making a top-down shooter game, but I can't get the bullets to actually travel once they're shot. They simply remain at the spawn point and no more than one bullet is present at any time, from what I could tell. The code I have is:
var bullet : Rigidbody; //Your bullet prefab. Should have a RigidBody
var speed : float = 10.0f;
var muzzlePoint : Transform; //Your SpawnPoint
function Update() {
if(Input.GetButtonDown("Fire1")) {
var instance : Rigidbody = Instantiate(bullet, muzzlePoint.position,
muzzlePoint.rotation);
instance.velocity = muzzlePoint.forward * speed;
}
}
The gun has an empty game object in front as the "muzzle". Is there an issue with the code? Or what do I do? Thanks in advance.
When they are spawned, are they too close to the muzzle? If your bullets are colliding with something after they are instantiated, then yes they will not move. Does your bullet have gravity enabled? I'm surprised they do not fall if they are. I also assume you tested raising the value of speed.
What happens if you hold down your fire button? : oliver, what kind of question is that? The answer is nothing, read the script :
if(Input.GetButtonDown("Fire1")) {
GetButtonDown : http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButtonDown.html
Sorry, but I really couldn't see the purpose of your comment.
Sorry for the late reply. I've tried moving the bullet around away from the muzzle, but no luck, no matter what the position is. Unless they're colliding with themselves, I still haven't figured out why it isn't working. Thanks for the suggestions, though.
I just tested your script on a new scene using an empty game object. It worked fine. So you need to step through your situation to see what is going on. Check for any errors output to the Console window. Add code to your bullet to see if it is colliding with something. Start from the $$anonymous$$imal setup I tested above and add items to build your scene. Etc.
After backtracking through a lot, I've retested it and it works fine. Not sure what went wrong, but it's working now. Thanks =]
Your answer
Follow this Question
Related Questions
Quick Bullet Instantiation Problem 2 Answers
Setting bullet instansiate direction? help? 1 Answer
shot delay in between bullets 1 Answer
Make a Gun fire multiple times 3 Answers
How can I shooting script c#? 0 Answers