Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Scene pivot not updating on middle mouse click

Discussion in 'Editor & General Support' started by drsalvation, Nov 24, 2023.

  1. drsalvation

    drsalvation

    Joined:
    May 2, 2014
    Posts:
    38
    Using Unity 2022.3.14f1
    it looks like tapping middle mouse button anywhere no longer works to switch the scene pivot point.
    -IT'S NOT MY MOUSE- My mouse works perfectly fine in and outside of unity, I can still press and hold middle mouse to drag the scene view around.
    I tried creating a new clean project, thinking maybe it's some package I had on my original project but it's the same issue.

    This means I'm not able to work on fine details in maps, and scrolling once makes a huge leap in distance, and even nearby objects are clipping before the camera gets near.

    I'm wondering if there's an alternative way to switch the scene pivot other than just middle click, or maybe a toggle to fix that in editor preferences?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,338
    I usually just select an object and press F to focus, or else perhaps click on terrain, both of which should reset the scene pivot... do those still work? If not, try reimporting all...
     
    koizhang likes this.
  3. drsalvation

    drsalvation

    Joined:
    May 2, 2014
    Posts:
    38
    yeah, I've been getting around pressing F to focus on a game object of smaller sizes, my scenes currently don't have any terrains, but even so, this also happens in fresh new projects, so it's not just the current project I'm working on.

    I'm mostly curious if this is a 2022.3.14f1 issue (since I wasn't having that issue on previous versions), or if something went wrong in my installation, or if it's just not a feature anymore in that version.
     
  4. drsalvation

    drsalvation

    Joined:
    May 2, 2014
    Posts:
    38
    I guess my question for everyone is if this is something that's not working in 2022.3.14f1 or if my own version is flakey and I need to reinstall or do something else.
     
  5. Abdullah-Alaskari

    Abdullah-Alaskari

    Joined:
    Apr 7, 2013
    Posts:
    12
    I am using 2023.2.1f1 and have the same issue.
    when I use 2021 LTS it is fine I did not test with 2022 however
     
  6. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,297
    So in 2021 LTS, holding Middle Mouse temporarily switches to the Hand tool. This allows you to drag to pan the scene viewpoint around.

    In 2022.3.14f1, holding Middle Mouse does not always automatically switch to the Hand tool. If you manually click the Hand tool, then you can drag and pan the scene. They broke it.
     
  7. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,297
    It seems like if you use any window other than the Scene view, then your Middle Mouse will just not work at all (even if the Hand tool is selected), until you specifically give the Scene view focus again. This is really a bad workflow snag. I don't want to click on stuff in the Scene, thus messing up whatever I already have selected. I just want to move the scene viewpoint around. If I am in 2D mode, I can't just Right Click to go to WASD Fly mode, like I could in 3D.
     
  8. drsalvation

    drsalvation

    Joined:
    May 2, 2014
    Posts:
    38
    So it's definitely not just something bad with my installation, but something that's affecting everyone with those versions... On one hand, I'm glad to know it's not just me, on the other hand, it's worse...
    I really hope this gets fixed soon, it's so annoying having to click and object and tap F, it's also very inconsistent.
     
  9. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,297
    Yeah, it's a crappy turn of events. After further experimentation:

    The mouse wheel will zoom your camera whether the Scene view has focus or not. This is true in all modern versions. You just have to have the mouse cursor within the Scene view area.

    In 2021 LTS, the middle mouse click (while the cursor is in the Scene view area) will switch focus to the Scene view, and grab your camera for panning if you hold and begin to drag the mouse.

    In 2022 LTS, the middle mouse click will NOT switch focus to the Scene view, and will NOT grab your camera when the Scene view does not currently have focus. A functionality regression.

    You can left- or right-click the Scene tab itself to put it into focus, so that the middle mouse panning can work. An annoying extra click, but it doesn't upset the selection or anything else.

    If your Scene view has the gray toolbar across the top (showing docked overlays like Pivot, Local, Grid, Magnet, and the various View Options like Render Mode, Lighting, Gizmo Toggle, etc.) then you can left- or right-click in that gray toolbar area to put the Scene view into focus. An annoying extra click, but it doesn't upset the selection or anything else.
     
  10. RHPOSITIVE

    RHPOSITIVE

    Joined:
    Nov 11, 2015
    Posts:
    7
    It's really annoying. But since it is a general problem, it will probably be fixed in future updates.
     
  11. DestrucSean

    DestrucSean

    Joined:
    Aug 2, 2017
    Posts:
    5
    I also noticed this in 2022.3.14f1. I can't speak for other versions of 2022 since this is my first attempt at using it, but I really miss the middle mouse button press as a re-pivot. Is it possible to manually reset this anywhere?
     
  12. necroEternal

    necroEternal

    Joined:
    May 5, 2018
    Posts:
    2
    Just updated to 2022.3.14f1 and this bugged the hell out of me since I use the pivot constantly. I decided to go on a short procrastination adventure and implemented a workaround:

    Code (CSharp):
    1.  
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. [InitializeOnLoad]
    6. public class SceneViewPivot {
    7.  
    8.     private static bool _setPivotOnMouseUp;
    9.  
    10.     static SceneViewPivot() {
    11.         SceneView.duringSceneGui += OnSceneGUI;
    12.     }
    13.  
    14.     private static void OnSceneGUI(SceneView sceneView) {
    15.         Event e = Event.current;
    16.  
    17.         if (e.type == EventType.MouseDown) {
    18.             _setPivotOnMouseUp = (e.button == 2);
    19.         } else if (e.type == EventType.MouseDrag) {
    20.             _setPivotOnMouseUp = false;
    21.         } else if (e.type == EventType.MouseUp && _setPivotOnMouseUp) {
    22.             UpdatePivot(sceneView, e.mousePosition);
    23.             _setPivotOnMouseUp = false;
    24.         }
    25.     }
    26.  
    27.     private static void UpdatePivot(SceneView sceneView, Vector2 mousePosition) {
    28.         Ray ray = HandleUtility.GUIPointToWorldRay(mousePosition);
    29.  
    30.         if (Physics.Raycast(ray, out RaycastHit hitInfo)) {
    31.             sceneView.pivot = hitInfo.point;
    32.             sceneView.size = (hitInfo.distance / 2.0f);
    33.         }
    34.     }
    35. }
    36.  
    This snaps instantly to the new pivot position instead of interpolating nicely like normal, but its better than nothing!
     
  13. drsalvation

    drsalvation

    Joined:
    May 2, 2014
    Posts:
    38

    You are awesome! This will definitely save me some frustrations!
     
    necroEternal likes this.
  14. iGoA

    iGoA

    Joined:
    Aug 23, 2020
    Posts:
    22
    Thanks a lot! Needed this, because I've larger objects (like meshes along a path) and focusing them just sets the scene view pivot to the center of the object. That doesn't help. I need to be able to set the scene view pivot manually. Great work. Don't get, why this feature disappeared in the first place.
     
    necroEternal likes this.
  15. Silly11

    Silly11

    Joined:
    Mar 14, 2014
    Posts:
    2

    Hi there!
    I certainly hope you don't mind, but I have taken your code and run it through the fun ChatGPT thing to get interpolation working. See below. It has the added bonus of being able to change the value of the "interpolationSpeed" value to change how quickly it snaps to the new pivot point.

    Thank you so much for your code - I hope this works well for you as it has for me, seeing as it seems to restore an apparently lost function in this version of Unity. It's been a remarkable resource for me and an incredible help. I wouldn't have this working if it wasn't for you, so.. Thanks again.

    Edit: Would have to wait for interpolation to complete before being allowed to move the scene view again. Fixed this by stopping the interpolation during other OnSceneGUI events if the camera is already interpolating. Silly ChatGPT (And Silly me.. I got excited to reply to this without doing enough testing).

    Note: This doesn't work as well as the inbuilt way that this function works. This will ignore some models in the scene - I am unsure if it ignores custom meshes or what, I don't really ever mess with editor scripts.

    Code (CSharp):
    1.  
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. [InitializeOnLoad]
    6. public class SceneViewPivot
    7. {
    8.     private static bool _setPivotOnMouseUp;
    9.     private static Vector3 targetPivot;
    10.     private static float interpolationSpeed = 0.1f; // Adjust the speed as needed
    11.     private static float interpolationStartTime;
    12.     private static bool isInterpolating;
    13.  
    14.     static SceneViewPivot()
    15.     {
    16.         SceneView.duringSceneGui += OnSceneGUI;
    17.         EditorApplication.update += Update;
    18.     }
    19.  
    20.     private static void OnSceneGUI(SceneView sceneView)
    21.     {
    22.         Event e = Event.current;
    23.  
    24.         if (e.type == EventType.MouseDown){
    25.             _setPivotOnMouseUp = (e.button == 2);
    26.  
    27.             if(isInterpolating){
    28.                 StopInterpolation();
    29.             }
    30.         }
    31.         else if (e.type == EventType.MouseDrag){
    32.             _setPivotOnMouseUp = false;
    33.  
    34.             if(isInterpolating){
    35.                 StopInterpolation();
    36.             }
    37.         }
    38.         else if (e.type == EventType.MouseUp && _setPivotOnMouseUp){
    39.             targetPivot = CalculatePivot(sceneView, e.mousePosition);
    40.  
    41.             interpolationStartTime = (float)EditorApplication.timeSinceStartup;
    42.             isInterpolating = true;
    43.  
    44.             _setPivotOnMouseUp = false;
    45.         }
    46.     }
    47.  
    48.     private static void Update()
    49.     {
    50.         if (isInterpolating){
    51.             SceneView sceneView = SceneView.lastActiveSceneView;
    52.  
    53.             if (sceneView != null){
    54.                 float distanceThreshold = 0.01f; // Adjust the threshold as needed
    55.  
    56.                 // Check if the distance between current pivot and target pivot is below the threshold
    57.                 if (Vector3.Distance(sceneView.pivot, targetPivot) < distanceThreshold){
    58.                     StopInterpolation();
    59.                     return;
    60.                 }
    61.  
    62.                 if (EditorApplication.timeSinceStartup - interpolationStartTime < 1f / interpolationSpeed){
    63.                     // Calculate interpolation percentage
    64.                     float t = (float)(EditorApplication.timeSinceStartup - interpolationStartTime) * interpolationSpeed;
    65.                     // Smoothly interpolate the pivot towards the target pivot
    66.                     sceneView.pivot = Vector3.Lerp(sceneView.pivot, targetPivot, t);
    67.                 }
    68.                 else{
    69.                     // Reset the interpolation flag when the interpolation is complete
    70.                     StopInterpolation();
    71.                 }
    72.             }
    73.             else{
    74.                 // Reset the interpolation flag if there is no active SceneView
    75.                 StopInterpolation();
    76.             }
    77.         }
    78.     }
    79.  
    80.     private static void StopInterpolation()
    81.     {
    82.         isInterpolating = false;
    83.     }
    84.  
    85.     private static Vector3 CalculatePivot(SceneView sceneView, Vector2 mousePosition)
    86.     {
    87.         Ray ray = HandleUtility.GUIPointToWorldRay(mousePosition);
    88.         if (Physics.Raycast(ray, out RaycastHit hitInfo)){
    89.             return hitInfo.point;
    90.         }
    91.         return sceneView.pivot; // Return the current pivot if no intersection is found
    92.     }
    93. }
    94.  
    95.  
     
    Last edited: Jan 16, 2024
  16. drsalvation

    drsalvation

    Joined:
    May 2, 2014
    Posts:
    38
    Thanks for the script! It prevents making successful builds, but it's as simple as commenting it out before making a build.
    I REALLY hope that Unity gets it together and fixes it by next update, I first noticed it on 2022.3.14 (if I recall correctly, it wasn't an issue in 13), I'm in 2022.3.16, still an issue.
     
  17. Silly11

    Silly11

    Joined:
    Mar 14, 2014
    Posts:
    2
    Interesting that it causes you to not be able to make a build! I made a build with it just fine, so I have literally no clue what might be going on there.
     
  18. drsalvation

    drsalvation

    Joined:
    May 2, 2014
    Posts:
    38
    I think it was related to wanting to execute editor code for a build, I'll have to revisit that again when I try my next build, but if it was that simple, I guess an "if editor" should help fix that issue