Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

OnMouseEnter and OnMouseExit fires continually in object borders.

Discussion in 'Scripting' started by Duclos, Oct 10, 2019 at 7:45 PM.

  1. Duclos

    Duclos

    Joined:
    Jan 31, 2014
    Posts:
    9
    I have a small script that modifies the appearance of the cursor when you mouse over an object. However, the cursor changes between the default and alternate cursor continuously when it is near the edge of the object.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ClickableObject : MonoBehaviour
    4. {
    5.     public Texture2D overCursor;
    6.     public Vector2 overCursorOffset;
    7.  
    8.     private CursorSettings defaultCursorSettings;
    9.  
    10.     private void Awake()
    11.     {
    12.         defaultCursorSettings = GameObject.Find("GameSettings").GetComponent<CursorSettings>();
    13.     }
    14.  
    15.     private void OnMouseEnter()
    16.     {
    17.         Cursor.SetCursor(overCursor, overCursorOffset, CursorMode.Auto);
    18.     }
    19.  
    20.     private void OnMouseExit()
    21.     {
    22.         Cursor.SetCursor(defaultCursorSettings.defaultCursor, defaultCursorSettings.defaultCursorOffset, CursorMode.Auto);
    23.     }
    24. }
    ezgif.com-gif-maker.gif

    (Blinks much faster than you can see in the gif)
     
  2. Duclos

    Duclos

    Joined:
    Jan 31, 2014
    Posts:
    9
  3. Duclos

    Duclos

    Joined:
    Jan 31, 2014
    Posts:
    9
    ok, I have been investigating and I have discovered that the problem disappears if the following two conditions are met:

    1) I delete the following line from my character's movement script:
    Code (CSharp):
    1. /*Where:
    2. private CharacterController controller;
    3. and
    4. private Vector3 movement;*/
    5. controller.Move(movement * Time.deltaTime);
    and 2) delete the NavMeshAgent from the character

    Any help now? Please? :(
     
  4. Duclos

    Duclos

    Joined:
    Jan 31, 2014
    Posts:
    9
    I have also discovered that if I disable the character's camera and change it to a static camera in the scene, the problem does not occur.