- Home /
Shooting a bullet/ projectile PROPERLY
I'm working on a FPS game, and have only a little knowledge about scripting and animations. I encountered a problem, spent all day long searching answers for it, still no hope. I attached this script :
var projectile : Rigidbody;
var speed = 20;
function Update () {
// Put this in your update function
if (Input.GetButtonDown("Fire1")) {
// Instantiate the projectile at the position and rotation of this transform
var clone : Rigidbody;
clone = Instantiate(projectile, transform.position, transform.rotation);
// Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection (Vector3.forward * speed);
}
}
into my camera, but when i press shoot, it shoots the bullet all around. Even if im aming at the same spot, sometimes it shoots upward, sometimes it shoots downwards, sometimes from the centre, sometimes from below, even from on top. How do i fix this problem?
Also, can anyone give me a scrip suitable for a gun that shoots using raycast? REALLY needed help, thanks.
Answer by Novodantis 1 · Jun 16, 2010 at 12:48 PM
Is it possible that you have a collider on your camera that is colliding with your projectile? Use Physics.IgnoreCollision(clone.collider, collider)
to ignore collisions between the fired object and the object doing the firing.
Edit: moved from comment to answer
You add this to the part of your code where you instantiate the bullet, any time after it has been created. The first value is the collider on the new object, "clone.collider"; and the second is the collider on the thing that fired it, "collider" (that is the one attached to the same object as the script).
Nope, its not the collision who caused this, because ive tried using an empty object attached with the script, and the result is the bullet falling below the ground..... this is really frustrating.....
If nothing is working still, try making the projectile from scratch: a sphere with the collider removed and a rigidbody added (gravity disabled).
thanks, finally got it working, but still, i have to set the speed to 10000 to get it working like rocket speed...
Answer by · Jun 16, 2010 at 10:49 AM
From the Unity Rigidbody.velocity documentation:
In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour.
Instead of setting the object velocity, add force to the object:
clone.AddForce(clone.transform.forward * speed);
I've not tried instantiating a Rigidbody before, only a Transform/GameObject prefab, but I can't think of a reason it wouldn't work. Try pasting this code over yours.
var projectile : Transform;
var bulletSpeed : float = 20;
function Update () {
// Put this in your update function
if (Input.GetButtonDown("Fire1")) {
// Instantiate the projectile at the position and rotation of this transform
var clone : Transform;
clone = Instantiate(projectile, transform.position, transform.rotation);
// Add force to the cloned object in the object's forward direction
clone.rigidbody.AddForce(clone.transform.forward * shootForce);
}
}
its still the same.... my real problem is that the projectile is being shot randomly... when im standing on a plane, sometimes the bullet even goes beneath the plane....
I suspected that adding a velocity directly to the Rigidbody was the cause of the random direction. Does your script look exactly like the code I've got in my answer? (updated to include the whole script)
By the way, i put the script inside the camera, which is a child of my controller
I've tested this locally, adding the script to the camera, and it works fine. I've changed the Rigidbody variables to Transform.
Can you try the script in the updated answer and let me know how it goes? Are there any scripts on the projectile prefab?
Answer by nari0015 · Oct 07, 2011 at 06:20 PM
Your Bullet will always fire at what it is attached to, so if you attach the bullet with the firing script, and a small invisible square without a mesh, then it should fire at, and through, the square
Answer by FizzyBear · Apr 09, 2012 at 11:14 AM
I changed this
clone.AddForce(clone.transform.forward * speed);
Into
clone.AddForce(clone.transform.forward * 1000);
And it worked :o
Answer by wilczek01 · Feb 27, 2013 at 10:44 PM
rigidbody.AddRelativeForce(Vector3.forward * Time.deltaTime * speed);
I hope that it works ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613062229im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
shoot projectile from weapon 2 Answers
How can I rotate a rigid body based on its velocity? 3 Answers
How to instantiate a projectile only from the weapon prefab of the firing player? 1 Answer
Could you advice me why this Gun and Ballistic Script doesn't work? 1 Answer
how to only be able to shoot if an object is active 2 Answers