- Home /
How to implement gun accuracy/spread with raycast
Right now whenever you shoot, the raycast always hits perfectly where you aim. I want to make it so that the raycast has variation to where it hits.
Like it would be nice if I could have a public variable like accuracy to change how accurate the gun shoots, but I don't know how to do that. This is in c#..........
void shoot () { currentAmmo--;
if (currentAmmo > 0)
{
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
if (hit.rigidbody != null)
{
ApplyHitForce();
}
Debug.Log(hit.transform.name);
Peeker_instakill peekerHead = hit.transform.GetComponent<Peeker_instakill>();
if (peekerHead != null)
{
peekerHead.takeDamage(damage);
}
Peeker_BodyPartHealth peekerBody = hit.transform.GetComponent<Peeker_BodyPartHealth>();
if (peekerBody != null)
{
peekerBody.takeDamage(damage);
}
}
}
}
}
Answer by Topthink · Jun 14, 2018 at 09:38 PM
Thinking out loud mostly. But consider measuring the distance and then come up with a spread-times-distance calculation. Maybe an expert (or level five shooter, etc) will have a variation of a certain distance at a certain range. The variation would be more for a novice shooter (maybe five times the variation of the expert -- or whatever works for you). You might have different levels of shooters with different variations … something like that. Best wishes, whatever you come up with.
Your answer
Follow this Question
Related Questions
FPS shooting 1 Answer
Problem in Shooting Accuracy 0 Answers
How do i make a Railgun style effect with a raycast shot? 1 Answer
How can I make a Grapple Gun work with the Character Controller? 1 Answer
How make Raycast Aim to Crosshair? 1 Answer