Hello, I have this script, How i can make an recoil?
if (Input.GetKey(KeyCode.Mouse0) && Time.time > singleNextFire)
{
GetComponent<AudioSource>().Play();
muzzleFlash.Stop();
muzzleFlash.Play();
singleNextFire = Time.time + fireRate;
Vector3 rayOrigin = raycastcamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
if (Physics.Raycast(rayOrigin, raycastcamera.transform.forward, out hit, maxRange))
{
Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
EnemyHealth enemy = hit.collider.GetComponent<EnemyHealth>();
if (enemy != null)
{
enemy.DamageEnemy(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * hitForce);
}
}
}
Maybe if i make if i make:
Vector3 rayOrigin = raycastcamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
To be randomly at 0.4 and 0.6 or an number between 0.4 - 0.6??