x


Instantiate not shooting in right direction

I am trying to make a gun shoot bullets, and I have this code:

var bulletcreate = Instantiate(bulletprefab, GameObject.Find("Cylinder").transform.position, Quaternion.identity);
bulletcreate.rigidbody.AddForce(transform.forward * bulletspeedm4);

I have the bullet shooting out of the right spot, but when I shoot, it shoots out to the left. I want it to shoot out straight, right in the center of the screen. also, the cylinder in the code is the point where the bullet spawns from, then launches forward.

more ▼

asked Dec 03 '10 at 04:21 AM

Kazaskater gravatar image

Kazaskater
80 16 16 23

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

1 answer: sort voted first

When you use Quaternion.identity you are making the object align with the world, to make it align with the GameObject that this script is on use transform.rotation instead.

Things that could mess you up are

  • your character/camera is not facing forward (positive Z axis)
    If your character is built facing in the negative X direction then
  • your script is on a game object that is not rotating with the camera (transform is the Transform of the GameObject that the script is on)
    to use the transform of the camera add a var mainCamera : Transform; property to your script and link it to the camera in the inspector.
more ▼

answered Dec 03 '10 at 04:53 AM

Rennat gravatar image

Rennat
664 5 8 18

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

x2155
x1667
x1525
x445
x436

asked: Dec 03 '10 at 04:21 AM

Seen: 2006 times

Last Updated: Dec 03 '10 at 04:21 AM