x


Spray Shot Script

I'm trying to make a bullet spray script but for some reason it only fires in a single direction, no matter how the character turns around. Any reason for this?

/// Add spread shot

    var rotation : Quaternion = Quaternion.identity;
    rotation.eulerAngles = Random.insideUnitSphere * 10;
    transform.rotation = rotation;
    var BulletShot = Instantiate(Bullet, transform.position, transform.rotation);
    BulletShot.rigidbody.AddForce(transform.forward * 1000);
more ▼

asked Feb 10 '11 at 04:25 AM

Persona gravatar image

Persona
246 74 85 96

Does the shot always shoot in the same direction in the worldspace or is it always shooting "forward" dependant on the character rotation?

Feb 10 '11 at 07:30 AM GesterX

Same direction in world space

Feb 10 '11 at 08:38 AM Persona
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Ok, if I get what your talking about... Then this should work:

 var gunRecoil : float;

 var BulletShot = Instantiate(Bullet, transform.position, transform.rotation);

 var randomX = Random.Range(-gunRecoil,gunRecoil);
 var randomY = Random.Range(-gunRecoil,gunRecoil);

 var dir = transform.TransformDirection(randomX,randomY,1000);
 BulletShot.rigidbody.AddForce(dir);

move the gunRecoil variable to the first line of the script.

Hope this helps!

Also, I would instead of setting force, set the velocity manually:

BulletShot.rigidbody.velocity = dir;

Also there might be some errors... I have not tried it in unity yet.

Hope this helps!

more ▼

answered Feb 15 '11 at 08:44 PM

3dDude gravatar image

3dDude
2.6k 65 76 103

Can you explain how this code works?

Feb 18 '11 at 04:08 PM Persona

Ok, well instead of trying to rotate the point where the bullets a created... Why not just move there velocity to a random x y? So what the code does is instead of moving forward it creates a direction that sets the x and y component to be a random value some where between -gunRecoil and +gunRecoil. I have never used rigidbody force to move bullets so I don't know what it should be... But I am guessing something like 100. Please trying setting it to a wide range of numbers to test it.

Hope this helps!

Feb 18 '11 at 06:04 PM 3dDude

Thanks for the help

Feb 19 '11 at 03:01 PM Persona

Your welcome! Thanks for the points :D

Feb 19 '11 at 05:50 PM 3dDude
(comments are locked)
10|3000 characters needed characters left

I guess this script is attached to your weapon?

By setting Transform.rotation the object will be rotated in world space. To rotate it relative to the parent use Transform.localRotation.
And second: you can assign eulerAngles but they don't wrap around automatically. You will get an error/warning when you try to set them to values outside [0..360]. Use the built in function Quaternion.Euler instead.

var rotation : Quaternion = Quaternion.Euler(Random.insideUnitSphere * 10);
transform.localRotation = rotation;
var BulletShot = Instantiate(Bullet, transform.position, transform.rotation);
BulletShot.rigidbody.AddForce(transform.forward * 1000);

ps. I like your way to calculate the spreading ;)
The z rotation is not really useful but a bullet don't have a top-side you have to keep up.
If this script is on your weapon, the weapon gets also rotated. if you don't want the z rotation, just set the z value to 0.

var rotation : Quaternion = Quaternion.Euler(Random.insideUnitSphere * 10);
rotation.eulerAngles.z = 0;
transform.localRotation = rotation;
[...]
more ▼

answered Feb 16 '11 at 04:12 AM

Bunny83 gravatar image

Bunny83
45.4k 11 49 207

It fires off to the side.

Feb 16 '11 at 02:21 PM Persona

Hey, did you try my script?

Feb 16 '11 at 02:56 PM 3dDude

Make sure that your instantiated objects don't spawn inside of another collider. Otherwise the objects will spread randomly. And to clear things up: forward have to be z-axis (blue), up is y-axis (green) and x-axis is right (red). When you test in the editor you can select the objects during the game. Switch to the scene view or tile the windows so you see both (game view and scene view).

Feb 16 '11 at 03:30 PM Bunny83

I made sure. It looks like the firepoint is off by about 45 degrees

Feb 16 '11 at 08:24 PM Persona
(comments are locked)
10|3000 characters needed characters left

A similar question has been answered before. Try this.

more ▼

answered Feb 16 '11 at 10:53 AM

Sebas gravatar image

Sebas
4k 12 18 45

The answer you gave on that question was similar to mine.

Feb 16 '11 at 02:57 PM 3dDude

I'm aware of that. But for the sake of keeping things tidy, I prefer to use existing questions/answers as a reference. It should remind the person asking the question to do a thorough search first.

Feb 17 '11 at 07:17 AM Sebas

That indeed would be nice...

Feb 18 '11 at 06:05 PM 3dDude
(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:

x329
x152
x3

asked: Feb 10 '11 at 04:25 AM

Seen: 1200 times

Last Updated: Feb 10 '11 at 08:38 AM