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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Fake mouse cursor issues with moving camera

Discussion in 'Scripting' started by Phorsaken, Jun 29, 2017.

  1. Phorsaken

    Phorsaken

    Joined:
    Dec 12, 2013
    Posts:
    27
    I have a pause camera that rotates around a closeup of my character while showing the options on the right. For controller support on PC I went ahead an faked a mouse pointer that is controlled with the left stick. It was a bigger pain than I thought getting it working but I did about 90%. So here is my code.

    Code (CSharp):
    1.  
    2. protected override MouseState GetMousePointerEventData(int id)
    3.         {
    4.             MouseState m = new MouseState();
    5.             var screenSpaceCursorPosition = pauseCamera.WorldToScreenPoint(m_VirtualCursor.position);
    6.  
    7.             // Populate the left button...
    8.             PointerEventData leftData;
    9.             var created = GetPointerData(kMouseLeftId, out leftData, true);
    10.  
    11.             leftData.Reset();
    12.  
    13.             if (created)
    14.                 leftData.position = screenSpaceCursorPosition;
    15.             //leftData.position = Input.mousePosition;
    16.  
    17.             //Vector2 pos = Input.mousePosition;
    18.             Vector2 pos = screenSpaceCursorPosition;
    19.             leftData.delta = pos - leftData.position;
    20.             leftData.position = pos;
    21.             leftData.scrollDelta = Input.mouseScrollDelta;
    22.             //leftData.button = PointerEventData.InputButton.Left;
    23.             eventSystem.RaycastAll(leftData, m_RaycastResultCache);
    24.             var raycast = FindFirstRaycast(m_RaycastResultCache);
    25.             leftData.pointerCurrentRaycast = raycast;
    26.             m_RaycastResultCache.Clear();
    27.  
    28.             // copy the apropriate data into right and middle slots
    29.             PointerEventData rightData;
    30.             GetPointerData(kMouseRightId, out rightData, true);
    31.             CopyFromTo(leftData, rightData);
    32.             rightData.button = PointerEventData.InputButton.Right;
    33.  
    34.             PointerEventData middleData;
    35.             GetPointerData(kMouseMiddleId, out middleData, true);
    36.             CopyFromTo(leftData, middleData);
    37.             middleData.button = PointerEventData.InputButton.Middle;
    38.  
    39.             PointerEventData.FramePressState selectState = PointerEventData.FramePressState.NotChanged;
    40.             if (Input.GetButtonDown("JoySubmit"))
    41.                 selectState = PointerEventData.FramePressState.Pressed;
    42.             else if (Input.GetButtonUp("JoySubmit"))
    43.                 selectState = PointerEventData.FramePressState.Released;
    44.  
    45.             m_MouseState.SetButtonState(PointerEventData.InputButton.Left, selectState, leftData);
    46.             m_MouseState.SetButtonState(PointerEventData.InputButton.Right, StateForMouseButton(1), rightData);
    47.             m_MouseState.SetButtonState(PointerEventData.InputButton.Middle, StateForMouseButton(2), middleData);
    48.  
    49.             return m_MouseState;
    50.         }
    51.  
    The issue seems to be the world space calculation. Using this code while the camera or player was moving makes the mouse over blink in and out as well as making them have odd and unpredictable location updates at times. If you stand perfectly still and I disable the code that spins the camera around the player it works great. Any thoughts on how I can counter this issue?

    -Alexander
     
  2. Phorsaken

    Phorsaken

    Joined:
    Dec 12, 2013
    Posts:
    27
    Still can't figure this one out. A button for instance will blink repeatedly when you mouse over it like there is a delay between frames when it calculates the screen space from world space. The more the movement the worse the problem. This does not happen with the real mouse cursor in the same menu. Am I approaching this wrong or anyone have any thoughts? Thanks.

    -Alexander