Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by MadManAdam · Aug 18, 2018 at 04:14 PM · scripting problemonmouseover

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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).


Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MadManAdam · Aug 18, 2018 at 07:08 PM 0
Share

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 !@#$%^.

avatar image JVene MadManAdam · Aug 18, 2018 at 08:41 PM 0
Share

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.

avatar image MadManAdam JVene · Aug 19, 2018 at 02:05 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

219 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges