Thursday, 5 March 2015

Unity Turtorial: 24 How can I find the distance from one object to another using raycasts?

Unity Turtorial: 24 How can I find the distance from one object to another using raycasts ?







Unity3D TUTORIAL: 24 Find the distance from one object to another!

THIS IS Java script: GOOD 4 2D GAMES AND 3D GAMES ! 
----------------------------------------­­­­­­­----------------------------------­-­-­-­-­-­-­------



DIRECT DOWNLOAD HERE


----------------------------------------­­­­­------------------------------------­-­-­-­-­-

THIS IS BACKUP  IN TEXT FORM  TUTORIAL 23

1. Create New java and name it :
DISTANCE

2. Copy & Past this text and save java :
----------------------------------------­­­­­------------------------------------­-­-­-­-­-

#pragma strict

 var target: Transform; // planet transform
 private var distance: float;
 
 
 function FixedUpdate(){
   // get current position:
   var myPos: Vector3 = rigidbody.position;
   // create ray from this object to the planet center:
   var ray: Ray = new Ray(myPos, target.position - myPos);
   var hit: RaycastHit;
   // do a Raycast that sees only the target collider:
   if (target.collider.Raycast(ray, hit, Mathf.Infinity)){
     // save distance to the surface in variable distance:
     distance = hit.distance;
   }
 }
 
 function OnGUI(){
   // show the distance with one decimal digit:
   
   GUI.Label(Rect(10,10,120,30), distance.ToString("F1"));
 }



----------------------------------------­­­­­------------------------------------­-­-­-­-­-

0 comments:

Post a Comment