x


Gun accuracy with Random.Range?

I am trying to make guns accurate by adding in a "Random.Range" referenced added to the "Vector3" direction my player is facing. Yet, I am unsure of how to do this.

function Fire() {
   if (Time.time > reloadTime + lastShot && clipsize > 0)
   {

      if (abletoshoot == 1) 
      {
         BroadcastMessage ("ShootAnimation");

         var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);

         instantiatedProjectile.velocity = transform.TransformDirection(Vector3(0, 0, initialSpeed));

         Physics.IgnoreCollision( instantiatedProjectile.collider, transform.root.collider);

         lastShot = Time.time;
         clipsize--;
      }
   }
}

Im not sure where to put in "Random.Range", and how to set the range etc., while still keeping the same parameters. Is anyone willing to show me how I could perform this?

more ▼

asked Oct 22 '10 at 01:40 AM

user-4565 (google) gravatar image

user-4565 (google)
23 9 10 18

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

1 answer: sort voted first

You will want to put Random.Range() in your code where you specify the direction to shoot the projectile.

 // Randomize the x and y direction of the projectile
 instantiatedProjectile.velocity = transform.TransformDirection(Vector3(Random.Range(min, max), Random.Range(min, max), initialSpeed));

Replace min and max with the actual values for the minimum and maximum values of the random direction.

more ▼

answered Oct 22 '10 at 02:34 AM

BinaryCaveman gravatar image

BinaryCaveman
792 6 10 22

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

x1175
x575
x446
x28

asked: Oct 22 '10 at 01:40 AM

Seen: 1139 times

Last Updated: Oct 24 '10 at 08:31 PM