x


Making a gun shoot

I have this script it is supposed to make a gun shoot but the bullet does not go forward here it is:

var projectile : Rigidbody; var speed = 50;

var fireRate = 0.11; private var lastShot = -10.0;

function Update () { if(Input.GetButtonDown("Fire1")){ if(Time.time > fireRate+lastShot){ clone = Instantiate(projectile, transform.position, transform.rotation); projectile.tag = "Bullet"; clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));

    lastShot = Time.time;
}

Destroy(clone.gameObject, 3); } }

more ▼

asked Mar 06 '12 at 03:33 AM

321everybodycares gravatar image

321everybodycares
26 1 3 3

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

1 answer: sort voted first

if it is a rigidbody then i would not use velocity and transform direction. I would use rigidbody.AddForce instead for example

take out:

clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));

and put in:

clone.rigidbody.AddForce(Vector3.forward * speed);

more ▼

answered Mar 06 '12 at 03:46 AM

DGArtistsInc gravatar image

DGArtistsInc
269 8 15 19

Vector3.forward will always return (0,0,1); we probably want to use something like transform.forward here, to launch our bullet in the direction the shooter is currently facing.

Mar 06 '12 at 04:33 AM rutter

What are you talking about? Its the Vector3 of the rigidbody so it has nothing to do with the direction the character is facing. It matters what direction the object doing the instantiating is facing. Commonly a gun model is the child of an empty GameObject and that empty GameObject has the script on it usually. And when an object is Instantiated it is facing the direction that that empty GameObject is facing. SO it will shoot in the direction of which the shooter is facing so long that the empty GameObject and the Shooter are facing the same Direction.

Mar 07 '12 at 03:51 AM DGArtistsInc

As described by Unity's documentation: AddForce() uses world coordinates, which implies that force in the direction (0,0,1) would push it along the world's z-axis. Perhaps you're thinking of AddRelativeForce()?

If this is code that's worked for you in the past, more power to you.

Mar 07 '12 at 05:19 PM rutter

yes it does work for me... every time... when it says it implies force on the direction(0,0,1) it does not imply force in the WORLD'S Z Axis it implies force on the OBJECT'S Z axis Causing it to move forward. I see the point yo are making however AddForce has always worked for me. AddRelativeForce can be used as well though

Mar 08 '12 at 03:21 AM DGArtistsInc
(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:

x11

asked: Mar 06 '12 at 03:33 AM

Seen: 429 times

Last Updated: Mar 08 '12 at 03:21 AM