• 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
Question by Harry64 · Dec 28, 2013 at 12:19 AM · inputmouserotatevector2motion

rotate a hand crank with circular mouse motion?

hello. I have here a little problem which I cannot solve. I have not really a good idea how I could achieve this in my game.

my Game should be a 3D first person horror game.

basically it should work like this.

I want that the mouse controls for the player are deactivated and activated for the crank so that I can control the crank. that would no problem.

but I want that the player needs to rotate his mouse in a circular motion to rotate the crank to wind someting up or open a door. like in this picture. alt text

with one axis it would be no problem because I can map that to a float. but like in the middle picture a circular motion needs X and Y ....

the crank on the right picture should only show how the crank could look like in my game.

what I already got coded was not that good. it was a empty that moves in its own vector2 and it is clamped to a distance to its parent. so I thought I could then just model a crank and let the Z axis look at the empty and then it would rotate with my mouse... but if I would rotate the mouse in a very small circle or would move the mouse from left to right it would instantly snap to the other side or it would rotate way too fast.... so that idea is not good.....

please help.

crank_motion.jpg (46.1 kB)
Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by sparkzbarca · Dec 28, 2013 at 12:39 AM

the way i would do this

create a mesh which is just a circular plane in your modeling program so you got a circle

tahts going to be your collider mesh

create an empty game object with that mesh collider attached and center it on your crank. it should basically be a circle the outside edge of which the crank handle would trace in movement. Got that?

Good

now your going to take the mouse position in word space and place it on the same plane as this collider of course. Since the mouse position is only 2d on the screen you get the third dimension (in this case the Z depth) by making it equal to the collider object Z.

Now you are going to do a raycast from the mouse position to the center of this mesh collider object.

that will give you info about the point on the edge of the collider closest to the mouse

with that info you now know what you need to know

you move the crank handle from current position to the closest point on bounds

if you like you can do this through a simple rotation around the Z axis

the angle to rotate is the angle between A & B where

A = crankknobpiece.transform.position - crank center point.transform.position

basiscally your going to need to know 2 things where the handle part of the crank is and where the actual shaft it rotates part is

B = nearest point on bounds so the raycasthit.point - crank center point

now you have

mouse - - - handle---crank center

and you can use vector3.angle(A,B) to find the angle between mouse and handle from crank center point

then you rotate around the z axis angle degrees and arrive

YAY

Comment

People who like this

0 Show 5 · 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 Harry64 · Dec 28, 2013 at 01:34 AM 0
Share

... ok... I think that is almost the same I already coded...... somewhat... but would't it then also rotate very fast if my mouse makes very small circles?

here look that is the script I use now... it has a parent that is the center of my crank so its an empty. then I have the object that is for now just a sphere.

 var Parent : Transform;
 var Obj : Transform;
 var Radius = 5;
 var Dist : float;
 
 var MousePosX : float;
 var MousePosY : float;
 
 var MouseMatrix : Vector2;
 var MouseOffset : Vector3;
 var NormOffset : Vector3;
 var Norm2Offset : Vector3;
  
 function Update()
 {
     MousePosX += Input.GetAxis ("Mouse X");
     MousePosY += Input.GetAxis ("Mouse Y");
     
     MousePosX = Mathf.Clamp (MousePosX, -5, 5);
     MousePosY = Mathf.Clamp (MousePosY, -5, 5);
     
     
     MouseMatrix = Vector2 ( MousePosX, MousePosY );
     ParentVec = Vector2 (Parent.position.x, Parent.position.y);
     MouseOffset = MouseMatrix - ParentVec;
     Obj.position.x = MousePosX;
     Obj.position.y = MousePosY;
      
     Dist = Vector2.Distance(Vector2(Obj.position.x, Obj.position.y), Vector2(Parent.position.x, Parent.position.y));
      
     if(Dist > Radius)
     {
         var norm = MouseOffset.normalized;
         NormOffset = norm;
         Obj.position.x = norm.x*Radius + Parent.position.x;
         Obj.position.y = norm.y*Radius + Parent.position.y;
     }
     
     if(Dist < Radius)
     {
         var norm2 = MouseOffset.normalized;
         Norm2Offset = norm2;
         Obj.position.x = norm2.x*Radius + Parent.position.x;
         Obj.position.y = norm2.y*Radius + Parent.position.y;
     }
 }
avatar image sparkzbarca · Dec 28, 2013 at 01:45 AM 0
Share

the way i described was to find the point nearest the mouse, the thing is unless the crank is really small, basically its going to take however long it takes you to draw a circle around the mouse.

I did forget to deal with yea that you'd have to make sure they were not inside the crank radius drawing small cirlces

for that i'd just do a collider.contains(mousepos)

if so dont turn the crank, so they have to draw a circle around the outside not the inside of the crank

avatar image Harry64 · Dec 28, 2013 at 03:58 AM 0
Share

is there really no other way to do this? I mean many games have done this... but on Google I did not find anything useful...

avatar image sparkzbarca · Dec 28, 2013 at 04:24 AM 0
Share

i mean the thing is the way i described isn't actually that hard, honestly you could bang out that code and debug it in maybe 45 minutes to an hour max.

most games that have that feature use it enough to justify an hour spent coding.

avatar image Harry64 · Dec 28, 2013 at 08:21 PM 0
Share

I have now played around again with that code from above and now it works like I want it. I have now 2 vesions made. one that uses the speed of my mouse to rotate and one that uses a SmoothDampAngle function with a fixed speed.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Rotating 2D Sprite on 1 Axis Using 2 Input Axes 0 Answers

How to rotate an object along the x axis with mouse input? 1 Answer

How to get mouse position with ray from camera??? 1 Answer

Rotate an object with angle limit 1 Answer

Apple Mouse Scroll Wheel 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