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!
Answer by JVene · Aug 18, 2018 at 06:53 PM
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. On$$anonymous$$ouseDrag works find. I was hoping to use On$$anonymous$$ouseOver 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... :( Like my whole project is !@#$%^.
If you get over the Vector3 errors and have the time, there are ways you can assume the initiation of a drag state without having to use the mouse button. OnOver could be made to "select" if you like, but only you can deter$$anonymous$$e the feasibility of a user interface you design.
I got the Vector3 thing figured out, thankfully. After I have my GameObject selected I should then move the game object in Update or FixedUpdate? I'm making a 2D game and am not using rigid bodies. I got it working pretty good now using FixedUpdate but, am going to try Update next. What other options are there? Thank you for all the help. The steps outlined above have really helped me think through all the steps.
Your answer
Follow this Question
Related Questions
Unity 5 FPS crouch camera is pushed upwards by the controller[SOLVED!] 1 Answer
Scripting error 1 Answer
[SOLVED]Removing gameobject from list don't change the index 1 Answer
Im trying to set a spawned enemy a transform on another script 0 Answers
Raycast detecting colliders issue w/ image depicting problem 0 Answers