- Home /
Question by
unity_XoThIOHd3Bx0Lw · May 18, 2020 at 01:41 AM ·
raycastraycasthithit.point
How to add a random number to my AddForce angle
I'm following a tutorial and there is a part where you add a force to an object when shooting it but it looks really unnatural so I am trying to add a random number to the angle of the AddForce.
void Shoot()
{
muzzleFlash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(hit.normal * impactForce); //Here
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
}
public void RandomGenerate()
{
TheNumber = Random.Range(-10, 10);
}
}
I, unfortunately, don't know how to do this and simply adding TheNumber to hit.normal doesn't work. Any help is appreciated.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612234608im_/https://answers.unity.com/themes/thub/images/avi.jpg)