Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

object position x,y,z on mouse down c#

Discussion in 'Scripting' started by Alex Cruba, Aug 20, 2013.

  1. Alex Cruba

    Alex Cruba

    Joined:
    Aug 16, 2011
    Posts:
    564
    Nothing more to say. I need the x,y,z values of an object I clicked to use the x,y to adjust the camera.
     
  2. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    transform.position
     
  3. delinx32

    delinx32

    Joined:
    Apr 20, 2012
    Posts:
    417
    I think you want something like this? After you get the mesh filter, you should be able to access the transform of the mesh. This happens inside of an editor of mine. If its at runtime, you probably have to get the camera from the current runtime camera instead.

    Code (csharp):
    1.  
    2. SceneView sv = SceneView.lastActiveSceneView;
    3.  
    4.         Vector3 mousePos = new Vector3(Event.current.mousePosition.x, sv.camera.pixelHeight - Event.current.mousePosition.y);
    5.  
    6.  
    7.         RaycastHit hit;
    8.  
    9.         var ray = sv.camera.ScreenPointToRay(mousePos);
    10.        
    11.         if (Physics.Raycast(ray, out hit))
    12.         {
    13.             MeshFilter filter = hit.collider.GetComponent<MeshFilter>();
    14.         }
    15.  
     
  4. Alex Cruba

    Alex Cruba

    Joined:
    Aug 16, 2011
    Posts:
    564
    additional... could I move the camera to a special position? I didn't found something in documentation. I've the transform.position.x z and want to move the camera to it. I guess it's a transform thing?
     
    Last edited: Aug 20, 2013