Search Unity

OnMouseEnter and OnMouseExit fires continually in object borders.

Discussion in 'Scripting' started by Duclos, Oct 10, 2019.

  1. Duclos

    Duclos

    Joined:
    Jan 31, 2014
    Posts:
    12
    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:
    12
    Nobody?
     
  3. Duclos

    Duclos

    Joined:
    Jan 31, 2014
    Posts:
    12
    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:
    12
    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.
     
  5. Duclos

    Duclos

    Joined:
    Jan 31, 2014
    Posts:
    12
    The camera is currently a child of the player character. Is it possible that there are micromovements that alter the functioning of the OnMouseEnter?

    Please help ...
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Judging by what you said makes it stop, the problem is most likely that the object is moving slightly every frame, causing those events to fire as it shifts in and out of the mouse position. Verify that by looking at the character's position during gameplay.

    Make sure that when there are no movement inputs, there is no movement.
     
  7. Duclos

    Duclos

    Joined:
    Jan 31, 2014
    Posts:
    12
    If i use:
    Code (CSharp):
    1. controller.Move(Vector3.zero);
    the problem remains.
    If i use :
    Code (CSharp):
    1. //controller.Move(Vector3.zero);
    The problem disapears. It very weird.
     
  8. Duclos

    Duclos

    Joined:
    Jan 31, 2014
    Posts:
    12
    Reolved!

    Finaly i changed the parameter Min Move Distance in the character controller from 0.001 to 0.01 and the problem is solved!!

    Thanxs jeffreyschoch for the clue!
     
    LiterallyJeff likes this.