x


Help with gun scripting!?

I started using Unity from yesterday and I have tried to script a gun to fire through java script and the bullets fire out sideways!? any help

Heres the Script

var BulletPrefab:Transform; var force : float = 6000;

function Update() { if(Input.GetButtonDown("Fire1")) { var bullet = Instantiate(BulletPrefab,

GameObject.Find("spawnPoint").transform.position,

GameObject.Find("Gun").transform.rotation);

bullet.rigidbody.AddForce(transform.forward * force);

} }

more ▼

asked Jun 29 '12 at 07:04 AM

Strap1996 gravatar image

Strap1996
0 1

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

1 answer: sort voted first

The rotation you're passing in there is wrong.

You're calling GameObject.Find, which returns the first object of all your objects that is named 'whatever'. And it's very slow.

You want to cache gameObject.Find (note the lowercase G) as local variables for performance reasons.

Either your gun's forward isn't forward or you're Finding the wrong object.

more ▼

answered Jun 29 '12 at 02:43 PM

Loius gravatar image

Loius
10.7k 1 11 41

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

x5087
x1953
x446
x80
x10

asked: Jun 29 '12 at 07:04 AM

Seen: 296 times

Last Updated: Jun 29 '12 at 02:43 PM