Calling OnMouseOver More Than Once Per Frame?

Can calling OnMouseOver be called more than once per frame? I’m having an issue where I’m having a GameObject follow the mouse position. If I move the mouse too fast it will go off the GameObject and than obviously the GameObject won’t continue following the mouse any more. How can I get around this issue?

Here is my current code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowMouseCursor : MonoBehaviour {

    float distance = 9;
    public Vector3 mouseStartPosition;

    void OnMouseOver() // this works at slow mouse movements, fast movements will put the cursor off the gameobject and it won't update fast enough
    {
            Vector3 mousePosition = new Vector3(Input.mousePosition.x - 150, Input.mousePosition.y + 150, distance); // the -150 and +150 as an offset to place the mouse cursor in the center of my GameObject. 
            Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
            transform.position = objPosition;
    }

}

Do I need to be looking at using Time.Delta time, or getting multiple mouse positions per frame (how would I do this) and subtracting them or what? Any help greatly appreciated! Thank you for your time!

There are several issues that come to mind, but I assume I don’t see all of the code involved.


First, the mouse messages are sent by the operating system to Unity in real time, not on a basis synchronized with Update or FixedUpdate. The specifics may be dependent on platform, so performance could differ among platforms and devices (the mouse itself may send at rates lower than 200 samples per second, perhaps as high as 1200 samples per second, depending on the mouse hardware and drivers). I don’t have confirmation about Unity (I have more experience in a variety of engines and API’s), but it is entirely possible that mouse traffic is independent and interrupting.


When I read your text it occurs to me that OnMouseOver is not the place to choose to move an object. You’ll need to study mouse interfaces more to get all of the intricacies, but generally moving an object requires both selection and drag, not the mouse over phase. This means there ought to be a shift in mode of operation, from that of mouse over (which is merely a detection) to that of selection (where the software now considers an object to have been grabbed for movement), to that of dragging (which is what your text describes). If those phases are honored, the problem you describe related to speed evaporates, because the object “catches up” naturally, and can’t be “dropped” over an “alignment” issue as you describe, until it is released (a mode of operation I didn’t yet mention, following the end of dragging). You need to formalize these concepts of selection, dragging and releasing to accomplish what your text describes, and should not rely upon mouse over as means of moving an object (there is never any hope that the object would remain within the target under all conditions otherwise).


Thanks. OnMouseDrag works find. I was hoping to use OnMouseOver because I don’t want the user to have to hold down the mouse button all the time while dragging things around. Right now I have bigger fish to fry because something got royally messed up and am getting vector 3 errors on all my scripts all at once… :frowning: Like my whole project is !@#$%^.