Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question would i could make a gameobject follow by mouse on editor mode?

Discussion in 'Editor & General Support' started by pgwqq, Nov 3, 2020.

  1. pgwqq

    pgwqq

    Joined:
    Sep 25, 2017
    Posts:
    2
    Hello,guys,

    I want to make a builder tool on editor mode(not runtime),so I want to make a prefab follow mouse to place.

    I check some scripts on runtime mode,the logic looks OK.

    When i use `Camera.main` to calc ray ,it works fine.

    but i notice on editor mode ,some `SceneView.GetAllSceneCameras()` ,i test it,but it can locate correct position.

    Thanks!

    ```
    Ray r = SceneView.GetAllSceneCameras()[0].ScreenPointToRay(Input.mousePosition);

    Vector3 p = GetIN(r.origin, r.direction, Vector3.zero, Vector3.up);

    PlacePrefab.transform.position = p;
    ```
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Is Input.mousePosition reporting the correct values in editor mode? I feel like I've read in the past that you need to use a different method to get mouse position for the Scene view.
     
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    It's a bit odd to get the mouse position in the editor scene view, but this worked for me:
    Code (CSharp):
    1. Vector3 mousePosition = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition).origin;
    You may also need to set mousePosition's Z-value to 0 if you're editing in 2D mode.
     
    PraetorBlue likes this.