Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by lingai · Jun 05, 2012 at 02:26 PM · positionmousedragpathdragging

Checking if mouse dragged from one point to another

Im tryingto check if the mouse was left clicked and passed over, lets say, position 2,0,0 to position -3,0,0, and then does an action. Like the mouse dragging over a specified path and that triggers an even. Any help is much appreciated...been at this all night lol.

Comment
Add comment · Show 1
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 kolban · Jun 06, 2012 at 12:02 AM 0
Share

When the mouse button is pressed, that can be detected with Input.$$anonymous$$ouseButtonDown(). This can set the value of a variable (say a boolean) to flag that the mouse is down. You can also record the mouse position when the mouse was clicked. When the mouse is released, you can detect that from Input.$$anonymous$$ouseButtonUp() which can change the variable again. I think this is the core of what you want.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Jun 06, 2012 at 12:22 AM

You could register the mouse position when the button is pressed and measure the distance when the mouse button is released, like this:

var startPos: Vector3;

function Update(){ if (Input.MouseButtonDown(0)){ startPos = Input.mousePosition; } if (Input.MouseButtonUp(0)){ var offset = Input.mousePosition - startPos; if (offset.magnitude > 5){ print("Mouse moved more than 5 pixels"); } } } But notice that this code is operating in screen space: the distance is measured in pixels, not in the 3D world units. If you want to analyse the movement in the 3D world, things become way more complicated: you should use raycasts to find the 3D position in the mouse down and the mouse up cases, and the results could be very hard to handle - only objects that have a collider are detected by a raycast, thus you could have a mouse down point without a mouse up position, or vice versa, or no point at all (if you clicked the sky, for instance).

Comment
Add comment · Show 1 · 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 lingai · Jun 06, 2012 at 07:59 AM 0
Share

kind of, but Im trying to have the mouse follow a line threw an object. Alot like if you were playing fruit ninja. Having that point of contact and tracing the slash until the mouse is button is lifted

avatar image
0

Answer by Driseus · Jun 05, 2012 at 11:14 PM

I think this could answer to your question, if i understand you right....

javascript :

 var pressed : boolean = false;
 
 function OnMouseOver(){
     if(Input.GetButtonDown("Fire1"))
         pressed = !pressed ;
 }

C# :

 bool pressed = false;

 //this script just sets pressed true if the mouse is over the object, 
 // this script is attached too the rest is up to you...

 void OnMouseOver(){
     if(Input.GetButtonDown("Fire1"))
         pressed = !pressed ;
 }
Comment
Add comment · Show 2 · 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 kolban · Jun 05, 2012 at 11:58 PM 0
Share

As far as I can tell from reading this script, what it will do is toggle the boolean value stored in the "pressed" variable when the left mouse button is pressed. $$anonymous$$ore than that ... the value of pressed will be true after the first mouse button down, false after the second and true again after the third (etc) which doesn't feel very useful.

avatar image Berenger · Jun 06, 2012 at 12:04 AM 0
Share

I may have been a little too quick when I corrected his code. There was two if(), one with pressed = false the other pressed = true. I assumed it was redundant, but now that I think about it, it might have been for Up and Down. Please wait Driseus answer to that, and sorry Driseus if I screwed up your code :(

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

6 People are following this question.

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

Related Questions

How To Drag any Cube Using Mouse Click and Moved Only 1 Unit?? 2 Answers

Game Object flies out of screen when dragged 1 Answer

Drag object along Z axis using mouse 1 Answer

How do i disable my buttons while dragging ScrollRect? 0 Answers

Dragging a GameObject with mouse 1 Answer


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