Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

raycasthit.normal sometimes returns zero

$
0
0
Hello, so here is my story, my "player" shoots a blast, the "blast" object has a trigger and when it is shot and hits an object with tag "enemy", it adds a force to the object relative to the normal of the hit. void OnTriggerEnter(Collider col) { if (col.gameObject.tag == "enemy") { RaycastHit hit; Physics.Raycast(transform.position, col.gameObject.transform.position, out hit, DistanceToEnemy(col.gameObject)); Debug.Log("Distance To Enemy: " + DistanceToEnemy(col.gameObject)); col.gameObject.GetComponent().GetHitEffect(gameObject, hit.normal); Destroy(gameObject); } } and here is the GetHitEffect(GameObject, Vector3) function code public void GetHitEffect(GameObject blastObj, Vector3 hitAngleVector) //hitAngleVector is the hit.normal { GetComponent().freezeRotation = false; GetComponent().AddForce(-hitAngleVector * 6, ForceMode.Impulse); } so my problem is that when i fire a blast object and it hits the enemy, sometimes, the hit.normal is zero and i get no force on the object, but other times it calculates the hit perfectly fine. your help is immensely appreciated.

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>