- Home /
Bullet Positioning
I have several weapons in my game, and im starting to make it so they can shoot. I have a Desert Eagle which works fine, but then I have an F2000 that shoots, but the bullets are instantiated somewhere below the camera. I attached the script to the actual gun object, and used the same script as the Deagle, except modified to create an F200 bullet. I will link the script below, but I do not know why the F2000bullet is being made at a different position than the Deaglebullet, even though the 2 scripts are identical.
var projectile : Rigidbody; var initialSpeed = 20.0; var reloadTime = .2; var ammoCount = 60; var clipsize = 30; private var lastShot = -10.0;
function Fire() {
if (Time.time > reloadTime + lastShot && clipsize > 0)
{
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
Physics.IgnoreCollision( instantiatedProjectile.collider, transform.root.collider);
lastShot = Time.time;
clipsize--;
}
}
Answer by · Oct 06, 2010 at 03:06 AM
Check the transform gizmo / root node / pivot point of the F2000 GameObject. Instantiating the bullet at 'transform.position' via a script attached to the gun means that it will be spawned in a different place for each gun.
You could expose a public Vector3 on the gun script to define a relative position for the bullets to be instantiated from.
public var barrelPosition : Vector3; // relative position of the barrel from the transform root
When you select one of the guns (that has this script attached), the barrelPosition will appear in the Inspector.
Then on the line where you instantiate the bullet, change it to:
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.TransformPoint( barrelPosition ), transform.rotation);
When you fire the bullet, the position is determined by TransformPoint (the position of the gun with the local, relative position of the barrel applied). Tune the barrelPosition to get it to a point you're happy with.
well the center pivot of the object is inside of the gun which I want, and I want the bullets to spawn at different places for each gun, cuz each gun has different places, heights, etc. that look good.
Then I'd suggest declaring and defining a public Vector3, and spawn the bullets from that position, rather than the transform.position ... let me know if you're having trouble with this.
How do I do that? Sorry im new to Unity and JS, im just slightly modifying the FPS tutorial scripts as I go.
ah yes, this works nicely. Yet I still dont see how my bullet didnt spawn in the right place in the first place. The code for the 2 guns is identical, and so is the object positioning :/
Answer by user-4565 (google) · Oct 07, 2010 at 01:55 PM
Thank you so much for you're time in helping me out, its nice to get an answer every once in awhile thats actually helpful and persistently helpful, rather than "use a public variable. search the forums first next time." :) Thank you a great much!
No worries. Remember to vote up answers that help you, and mark them correct (click the tick) if it solves your problem. Cheers!