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 Mattias-Wargren · Oct 17, 2011 at 06:09 AM · rotationmouse-dragy-axis

Drag to rotate GameObject

I am building a game where you are rotating some GameObjects. When you select the GameObject, a handle will appear. When you press and drag the handle (or GameObject) I would like to rotate only on the Y-axis the depending on the mouse position. I was thinking of using transform.LookAt but I can´t seem to get it correct. The code below is what I have used now, but it only rotates the GameObject depending on the mouse x position. I would like the player to really have control over the rotation. Any ideas?

alt text

alt text

 void OnMouseDown()
 {
     mouseReference = Input.mousePosition;
 }
     
 void OnMouseDrag()
 {
     Vector3 offset = (Input.mousePosition - mouseReference);
     transform.Rotate(new Vector3(0, offset.x * 0.005f, 0));
 }


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 alarm656 · Jul 02, 2015 at 09:04 AM 0
Share

Yeah, this is awesome. Thank you very much for who has answered and who has asked!

4 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by Mattias-Wargren · Oct 17, 2011 at 06:14 PM

I solved it like this:

 using UnityEngine;
 using System.Collections;
 
 public class ObjectRotator : MonoBehaviour 
 {
     
     private float _sensitivity;
     private Vector3 _mouseReference;
     private Vector3 _mouseOffset;
     private Vector3 _rotation;
     private bool _isRotating;
     
     void Start ()
     {
         _sensitivity = 0.4f;
         _rotation = Vector3.zero;
     }
     
     void Update()
     {
         if(_isRotating)
         {
             // offset
             _mouseOffset = (Input.mousePosition - _mouseReference);
             
             // apply rotation
             _rotation.y = -(_mouseOffset.x + _mouseOffset.y) * _sensitivity;
             
             // rotate
             transform.Rotate(_rotation);
             
             // store mouse
             _mouseReference = Input.mousePosition;
         }
     }
     
     void OnMouseDown()
     {
         // rotating flag
         _isRotating = true;
         
         // store mouse
         _mouseReference = Input.mousePosition;
     }
     
     void OnMouseUp()
     {
         // rotating flag
         _isRotating = false;
     }
     
 }
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 c6y · Jan 09, 2014 at 03:09 AM 0
Share

Thanks for this $$anonymous$$attias! I was able to use the script, almost without changes. :)

avatar image justinpatterson · Feb 21, 2014 at 10:22 PM 0
Share

You're the man $$anonymous$$attias. That did the trick.

I made the following modification to make it rotate along both axes:

 void Update(){
         if(_isRotating)
         {
             // offset
             _mouseOffset = (Input.mousePosition - _mouseReference);
             // apply rotation
             //_rotation.y = -(_mouseOffset.x + _mouseOffset.y) * _sensitivity;
             _rotation.y = -(_mouseOffset.x) * _sensitivity;
             _rotation.x = -(_mouseOffset.y) * _sensitivity;
             // rotate
             //transform.Rotate(_rotation);
             transform.eulerAngles += _rotation;
             // store mouse
             _mouseReference = Input.mousePosition;
         }
 }
avatar image justinpatterson · Feb 23, 2014 at 11:21 PM 0
Share

Hm my code still has an issue with rotating the cube correctly once you rotate to a Z-axis position. Since it seems to only bother with applying rotation to Y and X axes, it gets really wonky once you get to the Z side and attempt to go left/right/up/down. I'll look into it more and post a solution if I find it.

avatar image tomekkie2 justinpatterson · Jan 14, 2016 at 11:18 AM 0
Share

@justinpatterson - I got around this issue by making a container. I make the container on$$anonymous$$ouseDown, center it to the GameObject, adjust to the camera by making the container.transform to LookAt the camera.transform, then parent the GameObject to the container and rotate the container. On $$anonymous$$ouseUp I release the GameObject from the container. This seems to work well.

avatar image Batka · Apr 10, 2018 at 06:44 PM 0
Share

I want to add that this works with a rigidbody, if anyone is looking.

  //Rotate around X
  _rigidbody.AddTorque(rotation.x);


avatar image
0

Answer by kromenak · Oct 17, 2011 at 06:57 AM

You might be able to actually track the change in the current drag angle around the object and react accordingly. Something like this:

 Vector3 startDragDir;
 Vector3 currentDragDir;
 Quaternion initialRotation;
 float angleFromStart;

 void OnMouseDown()
 {
     startDragDir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

     initialRotation = transform.rotation;
 }

 void OnMouseDrag()
 {
     currentDragDir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

     //gives you the angle in degrees the mouse has rotated around the object since starting to drag
     angleFromStart = Vector3.Angle(startDragDir, currentDragDir);

     transform.rotation = initialRotation;
     transform.Rotate(0.0f, angleFromStart, 0.0f);
 }

I haven't actually tested this code, so it might take some tweaking, but I think this would work. I used this sort of code in an iphone game where you rotate things by dragging your finger around an object.

Comment
Add comment · 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
0

Answer by Jeeva3m · Oct 17, 2016 at 01:57 PM

Hi @MattiasWargren and @justinpatterson Your script works fine for the object rotation on mouse click-drag. I've been trying to control minimum and maximum rotation angles on both x and y. Consider I can rotate and view an objects top view, but dont wanna see the bottom. Same way I can rotate and see front left and right side views but dont wanna rotate to see backside. How to control and set Minimum(x and Y) and Maximum(x and y) angles in the script. (Note: I dont wanna rotate camera rather only object rotation) Please Help

Comment
Add comment · 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
0

Answer by stbn0 · Apr 25, 2019 at 10:30 PM

Hi @Mattias-Wargren , thanks for this, it's been really helpful

Is it posible to limit the speed if the dragged distance is too high ?

I need to rotate the object but the velocity of the rotation should be limited

Thanks!!!

Comment
Add comment · 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

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

12 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

Related Questions

Mouse rotation with limits: limit overrun protection works on positive overruns only? 0 Answers

How can I rotate a gameobject with mouse drag? 1 Answer

Rotating and balancing HingeJoint2D with mouse drag 0 Answers

mouse x not working when mouse y in use? 1 Answer

More specific Quaternion.LookRotation 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