x


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.

more ▼

asked Jun 16 '10 at 09:52 AM

user-3061 (yahoo) gravatar image

user-3061 (yahoo)
126 29 29 37

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

5 answers: sort voted first

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);
    }
}
more ▼

answered Jun 16 '10 at 10:49 AM

Marowi gravatar image

Marowi
4.9k 4 14 53

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....

Jun 16 '10 at 11:11 AM user-3061 (yahoo)

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)

Jun 16 '10 at 11:39 AM Marowi

its exactly the same..

Jun 16 '10 at 11:43 AM user-3061 (yahoo)

By the way, i put the script inside the camera, which is a child of my controller

Jun 16 '10 at 11:45 AM user-3061 (yahoo)

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?

Jun 16 '10 at 12:06 PM Marowi
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jun 16 '10 at 12:48 PM

Novodantis 1 gravatar image

Novodantis 1
1.7k 14 22 40

how do i put it inside the script?

Jun 16 '10 at 12:49 PM user-3061 (yahoo)

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).

Jun 16 '10 at 12:51 PM Novodantis 1

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.....

Jun 16 '10 at 12:56 PM user-3061 (yahoo)

If nothing is working still, try making the projectile from scratch: a sphere with the collider removed and a rigidbody added (gravity disabled).

Jun 16 '10 at 01:10 PM Novodantis 1

thanks, finally got it working, but still, i have to set the speed to 10000 to get it working like rocket speed...

Jun 16 '10 at 01:21 PM user-3061 (yahoo)
(comments are locked)
10|3000 characters needed characters left

I changed this

clone.AddForce(clone.transform.forward * speed);

Into

clone.AddForce(clone.transform.forward * 1000);

And it worked :o

more ▼

answered Apr 09 '12 at 11:14 AM

FizzyBear gravatar image

FizzyBear
67 4 10 12

(comments are locked)
10|3000 characters needed characters left
rigidbody.AddRelativeForce(Vector3.forward * Time.deltaTime * speed); 

I hope that it works ;)

more ▼

answered Feb 27 at 10:44 PM

wilczek01 gravatar image

wilczek01
1

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

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

more ▼

answered Oct 07 '11 at 06:20 PM

nari0015 gravatar image

nari0015
1

(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:

x5049
x251
x217

asked: Jun 16 '10 at 09:52 AM

Seen: 24064 times

Last Updated: Feb 27 at 10:44 PM