So I have a little piece of code that will raycast from my weapon and instantiate some sparks at the place the ray has hit.
The problem is my firerate, if I make a smaller fire rate like 0.1, the sparks will start to appear on different places and not only where I aim. What could be causing the problem. I don’t even know what to search for in the forums, nothing makes sence. Here’s my code:
var hit : RaycastHit;
var dir = transform.TransformDirection (1, 0, 0);
if((Input.GetKey("f")) (fireRate < Time.time))
{
//if we hit something
if (Physics.Raycast (transform.position, dir, hit, 1000)) {
point = hit.point;
explosionRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
if(hit.rigidbody)
{
hit.rigidbody.AddForceAtPosition (-hit.normal * 900, hit.point);
Instantiate(impactMetal, hit.point, transform.rotation);
}
Instantiate(explosion, point, explosionRotation);
}
animation.Play ("shoot");
muzzleTime = Time.time + 0.1;
fireRate = Time.time + 0.1;