Search Unity

Raycast from scene camera - how to make it work in prefab mode

Discussion in 'Editor & General Support' started by marcrem, Mar 6, 2021.

  1. marcrem

    marcrem

    Joined:
    Oct 13, 2016
    Posts:
    340
    Hi,

    I have this neat little script that allows me to do CTRL+T to snap an object to the raycasted location at the mouse position on screen in my scene.

    It's not working in prefab mode, and I'd like to make it work. Any idea?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5. using UnityEditor.SceneManagement;
    6.  
    7. public class OUTBRKShortcuts : MonoBehaviour
    8. {
    9.     private static bool isMoving;
    10.  
    11.     [MenuItem("Tools/OUTBRK/Snap Object To Mouse Position %t")]
    12.     static void SnapObjectToMousePosition()
    13.     {
    14.         isMoving = true;
    15.  
    16.         SceneView.duringSceneGui -= UpdateSceneView;
    17.         SceneView.duringSceneGui += UpdateSceneView;
    18.     }
    19.  
    20.     private static void UpdateSceneView(SceneView sceneView)
    21.     {
    22.         if (isMoving)
    23.         {
    24.  
    25.             Vector3 pointPos = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition).origin;
    26.             Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
    27.            
    28.             RaycastHit hitInfo;
    29.  
    30.             if (Physics.Raycast(worldRay, out hitInfo, 10000))
    31.             {
    32.                 if(hitInfo.collider.gameObject != null)
    33.                 {
    34.                         // Move Selected Objects to raycast hit
    35.                         if (Selection.gameObjects.Length > 0)
    36.                         {
    37.                             foreach (GameObject go in Selection.gameObjects)
    38.                             {
    39.                                 Undo.RecordObject(go.transform, "Move Object to Mouse Position");
    40.                                 go.transform.position = hitInfo.point;
    41.                             }
    42.                             EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
    43.                         }
    44.                 }
    45.             }
    46.  
    47.             EditorWindow view = EditorWindow.GetWindow<SceneView>();
    48.             view.Repaint();
    49.  
    50.             isMoving = false;
    51.         }
    52.         else
    53.         {
    54.             SceneView.duringSceneGui -= UpdateSceneView;
    55.         }
    56.     }
    57. }
    58.  
    Thanks!
     
  2. angelonit

    angelonit

    Joined:
    Mar 5, 2013
    Posts:
    40
    I just had a similar problem and HAD to put the prefab in a scene, do the raycast operations there and save the changes on the prefab from there. Seems like Raycasts (of various types, at least Physics.Raycast and Physics.RaycastAll) do not work on prefab mode
     
    mikelortega likes this.