x


Displaying Coordinates in scene

Hi i want to create a function that when i click on a location or an objects on the terrain, i wanted the (coordinates) of it to be display on side of the scene,can some1 help me with it,currently i have this script which display coordinates on left button which outside scene,so how am suppose to do it?

function Update ()

{

if ( Input.GetMouseButton(0) )

{

  var hit : RaycastHit;

  var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);

  if (Physics.Raycast (ray, hit, 100.0))

  {

     var hitPos : Vector3 = Camera.main.WorldToScreenPoint(hit.point);

     Debug.Log(hitPos);

  }

}

}

more ▼

asked Jan 10 '12 at 01:23 AM

wenhua gravatar image

wenhua
151 31 42 44

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
private var hitPos : Vector3 ;
private var hitObjectPos : Vector3 ;
private var hit : RaycastHit ;

function Update(){

   if(Input.GetMouseButtonDown(0)){
 
      var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition) ;
      
      if(Physics.Raycast(ray , hit)){
         hitPos = hit.point ;
         hitObjectPos = hit.transform.position ;
      }
   }
 }

   //I guess you're wanting to use GUI to display this stuff??? 
   function OnGUI(){
       //The world position of the ray's contact point->
      GUI.Box(Rect(5,5,100,50), "Ray Hit Vector3 = " +hitPos) ;
      // The world position of the object the ray hits->
      GUI.Box(Rect(5,105,100,50), "Hit Object Vector3 = " +hitObjectPos) ;
   }

Give that a shot, see if that's something like you're trying for.

more ▼

answered Jan 10 '12 at 06:54 AM

Lo0NuhtiK gravatar image

Lo0NuhtiK
3.5k 1 9 39

Great its works,Thx very much LoONuhtiK

Jan 10 '12 at 08:02 AM wenhua

Please wenhua, if the issue is closed, click on the green button to mark the question as "solved". Thanks!

Jan 10 '12 at 08:05 AM BiG

Ok sure but wheres the green button?? lol new here^^.sorry for that. by the way LoONuhtiK,Do you help me 1 more time, acturally this script i will be using in my other question,but i dunno how to do it,do you mind go to my question on (Detect sign on 2D Fix Mini map,When click on 3D map,just like pin point)so on the same time i can display the coordinates together with the pin points

Jan 10 '12 at 08:15 AM wenhua

The accept button is a checkmark or something by the vote-up buttons.
Yeah, I can try to help on the other thing @wenhua

Jan 10 '12 at 08:22 AM Lo0NuhtiK
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x151

asked: Jan 10 '12 at 01:23 AM

Seen: 550 times

Last Updated: Jan 10 '12 at 08:22 AM