Skip to main content

Use of Raycast to determine certain game objects ?

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);             }         }  }

Find the Distance Between Two Game Objects in Unity 3D

Take a two Game Objects ObjA & Obj and write script as Follow :


public Transform ObjA;

public Transform ObjB;

float FindDistanceBetweenObjects()

{

        float _dis = Vector3.Distance(ObjA.position, ObjB.position);

        return _dis;

 }

 void Update()

{

        float Distnace = FindDistanceBetweenObjects();

        print("Distnace" + Distnace);

  }

Comments