- Home /
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?
Answer by BinaryCaveman · Oct 22, 2010 at 02:34 AM
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.
Your answer
Follow this Question
Related Questions
Weapon random movement 0 Answers
Aiming down sights 4 Answers
FPS Gun Recoil (Accuracy) 1 Answer
How to Implement First Shot Accuracy? 2 Answers
Gun Random Rotation 0 Answers