Quantcast
Channel: Latest Questions by Kenned
Viewing all articles
Browse latest Browse all 14

Physics.SphereCast or a sphere trigger area?

$
0
0
Hey everyone :) I'm currently working on the interaction in my game, however I have a small problem, since I thought it would be easier to just spherecast whenever I press the interaction button, however when I spherecast it seems to make the game lag quite a bit. Now I'm wondering if it would be more optimal to have a sphere collider marked as trigger, then use the OnTriggerStay instead. Which is the most optimal way? :3 Also, here is my code: void Interaction(){ RaycastHit hit = new RaycastHit(); Physics.SphereCast(transform.position,0.5f,transform.forward, out hit); interactionTimer = 0.0f; if(hit.collider != null){ if(Vector3.Distance(hit.collider.gameObject.transform.position,transform.position) <= interactableDistance){ switch(hit.collider.gameObject.tag){ case "NPC": hit.collider.gameObject.GetComponent().Action(); print ("YEAH!"); break; case "Chest": hit.collider.gameObject.GetComponent().Action(); break; case "Interactable": hit.collider.gameObject.GetComponent().Action(); break; } } } }

Viewing all articles
Browse latest Browse all 14

Trending Articles



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