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 Paul Graystone · Mar 18, 2013 at 10:34 AM · rotationmouseonmousedownworldtoscreenpoint

Rotate object about x-axis with mouse like in editor

I want to rotate an object like one can rotate an object in the editor.

The user performs a click on the object (anywhere). By holding the mouse button down she is then able to rotate the wheel (see image) about its x-axis. The rotation should feel like the rotation one can perform in the unity editor.

How can I achieve that? Any Help is appreciated :)!

alt text

EDIT: The coordinates of the mouse position are given and can be used as Vector2 MousePos.

unity.jpg (53.0 kB)
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
0

Answer by robertbu · Mar 18, 2013 at 02:55 PM

The simplest is to use Input.GetAxis():

var sensitivity = 5;

 function OnMouseDrag() {
     transform.Rotate(Vector3(sensitivity * Input.GetAxis("Mouse Y"), 0, 0));
 }

But you can use mousePos. I've use ScreenToViewportPoint() to make the code resolution indenpendent. It is not necessary. Replace with direct assignment and adjust the sensivity way down.

 var sensitivity = 300.0;
   
 private var lastPos : Vector3;
 
 function OnMouseDown() {
     lastPos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
     }
 
 function OnMouseDrag() {
     var pos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
       transform.Rotate((pos.y - lastPos.y)*sensitivity, 0, 0);
       lastPos = pos;
 }
Comment
Add comment · 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 Paul Graystone · Mar 18, 2013 at 04:08 PM 0
Share

As simple as it is. It works. But I have one issue. When rotating the object with the mouse I loose the object, meaning: The object rotates too slow compared to my mouse movement.

I know I can adjust sensitivity but putting in a value of >500 of even >1000 doesn't seem like an elegant solution. Is there a better way to fit mouse movement to the rotation speed (regardless of screen solution)? Or is the sensitivity solution the best practice? Thank you :)!

avatar image robertbu · Mar 18, 2013 at 04:13 PM 0
Share

Setting the sensitivity is just fine unless the object is moving relative to the camera (i.e. it is a knob in the scene that a character can be difference distance from). In that case I'd use world coordinates. Note if you were dragging an object rather than rotating, there would be a correct way of marrying the mouse to the object. But in rotating, for me, it is just what feels right.

avatar image Paul Graystone · Mar 18, 2013 at 05:58 PM 0
Share

So I guess sensitivity is the best practice for this kind of object manipulation. Is the sensitivity value dependent on the screen resolution?

In fact the player can be at difference distance and angles from the object. In which way must the code be modified to allow object rotation from any distance and especially from any angle?

avatar image robertbu · Mar 18, 2013 at 06:20 PM 0
Share

In the second code above, sensitivity is not related to screen resolution as long as the aspect of the game remains the same. Viewport coordinates start at (0,0) in the lower left and go to (1,1) in the upper right no matter what the resolution of the screen. If you end up with an aspect change, there will be some change in the X sensitivity, but not the 'Y'.

If you have a scene object, then you could use world coordinates ins$$anonymous$$d of viewport coordinates. There are several ways to do this. One way is to do calculate the two positions above (lastPos and pos) using this code (untested):

 var dist = $$anonymous$$athf.Abs(Vector3.Distance(target.transform.position - Camera.main.transform.position)).
 var pos = Input.mousePosition;
 pos.z = dist;
 pos = Camera.main.ScreenToWorldPoint(pos);

avatar image Paul Graystone · Mar 20, 2013 at 09:46 AM 0
Share

Thank you for the world coordinate solution. I will give it a try and will tell you if it has worked :).

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

10 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

Related Questions

Rotation of Object on single axis in direction of the mouse position 0 Answers

Slerp/Rotational Problem Query 1 Answer

Object Look At Mouse 2 Answers

Smooth camera rotation on mobile and pc when GetMouseButton()? 0 Answers

Problem with rotation script. 3 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