Raycast can be use in many fire like : There are many raycast type but these two are most that i used. 1.Physics.Raycast Player shooting/Fire 2.Physics.SphereCast Detect A object to Apply force due to explosion effect. Simple Physics Raycast as shown below : RaycastHit hit; Ray ray; void Update () { if ( Input .GetMouseButton(0)) { ray = Camera .main.ScreenPointToRay( Input. mousePosition); if ( Physics .Raycast(ray, out hit)) { Debug .Log( "Ray cast hit too " + hit.collider.gameObject.name); } } }
There will be many Situation where you have to find the nearest Enemy or nearest object from a player or a object. For this this below code is useful ; void Update () { Collider [] c = Physics .OverlapSphere(transform.position, 20); Transform t = GetClosestEnemy(c); print( "Nearest Object Name" + t.gameObject.name); } Transform GetClosestEnemy( Collider [] enemies) { Transform tMin = null ; float minDist = Mathf .Infinity; Vector3 currentPos = transform.position; foreach ( Collider t in enemies) { float dist = Vector3 .Distance(t.transform.position, currentPos); if (dist < minDist) { tMin =...