1
\$\begingroup\$

I would like to create an editor tool who can change the position of an object in the scene view into other object in the world, based on mouse position, like in the picture bellow (the red pyramid is my object to positionnate/rotate)

I would like to do a raycast from the scene view camera to the mouse position (or something like that), to get the gameObject over the mouse, and then get the position of the verticle / normal for position and rotation of my gameObject

enter image description here

For now, I have tryed some code who didn't work, I have my object named "preview", and inside the methode OnSceneGUI() in a editor script i have managed to move my preview object, but now I have to know where to put it... Thanks !

EDIT: here some of my try:

Transform previewPoint; //here my transform

void OnSceneGUI()
{
   PointCreator();
}

 private void PointCreator()
  {
  //test 1
      Camera sceneCam = SceneView.GetAllSceneCameras()[0];
      Vector3 spawnPos = sceneCam.ViewportToWorldPoint(Event.current.mousePosition);
      previewPoint.position = spawnPos;

      //test 2
      Vector3 mousePosition = Event.current.mousePosition;
      mousePosition.y = SceneView.currentDrawingSceneView.camera.pixelHeight - mousePosition.y;
      mousePosition = SceneView.currentDrawingSceneView.camera.ScreenToWorldPoint(mousePosition);
      mousePosition.y = -mousePosition.y;

      previewPoint.position = mousePosition;
      
  }

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

Ok, After a lot of research, I finnalt got this to work:

Transform objectPreview;  //the transform to move
Transform objectTouched = null; //the reference of the last object hit

Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);

RaycastHit hitInfo;

if (Physics.Raycast(worldRay, out hitInfo, Mathf.Infinity))
{
    if (hitInfo.collider.gameObject != null)
    {
        objectTouched = hitInfo.collider.gameObject;
        objectPreview.position = hitInfo.point;
        objectPreview.rotation = hitInfo.normal;
    }
}

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .