x


Shooting script not working properly

I'm using this script to shoot:

var bulletPrefab:Transform;

function Update () 
{

 if(Input.GetButtonDown("Fire1"))

   {

     //Create bullet
     var bullet = Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position, Quaternion.identity);

  //Direction of bullet and add force
  bullet.rigidbody.AddForce(transform.forward * 5000);   

   }
}

But it does not shoot properly. When I turn right it does not shoot, it just creates the bullet in front of the gun. Also when I shoot at certain angles it does not shoot to where I am aiming it shoots to the left. Besides this it works fine. What am I doing wrong?

more ▼

asked Jul 23 '12 at 08:13 PM

Sandr0G gravatar image

Sandr0G
40 2 7 10

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

3 answers: sort voted first

You need to change "Quaternion.identity", which aligns the rotation to the world axis, to "transform.rotation" or "spawnPoint.transform.rotation"

and maybe change "transform.forward" to "bullet.transform.forward"

more ▼

answered Jul 23 '12 at 08:19 PM

Seth Bergman gravatar image

Seth Bergman
7k 10 16 28

Thank you, it worked!

Jul 23 '12 at 08:24 PM Sandr0G
(comments are locked)
10|3000 characters needed characters left

hi!

the spawnPoint is a bit forward the gun and is a child of maincamera?

more ▼

answered Jul 23 '12 at 08:20 PM

Ingen gravatar image

Ingen
145 3 11 19

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

Hi i was using this script var bulletPrefab:Transform;

function Update () {

if(Input.GetButtonDown("Fire1"))

{

 //Create bullet
 var bullet = Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position, transform.rotation);

//Direction of bullet and add force bullet.rigidbody.AddForce(bullet.transform.forward * 5000);

} } first of all i cant drag a bullet prefab to inspector of my rifle and it wont shoot

more ▼

answered Jan 19 at 05:23 PM

Hassam1957234 gravatar image

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

x446
x329
x274

asked: Jul 23 '12 at 08:13 PM

Seen: 460 times

Last Updated: Jan 19 at 05:23 PM