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 /
avatar image
0
Question by Solu9 · Sep 15, 2016 at 04:20 AM · rotatemousepositioncircular

Rotate an object accordingly to the angle from object to mouse position

So I want to rotate a box towards the mouse position. When I circle the mouse around the box the box should rotate along with the mouse. I should note that I use orthographic view but with 3d models.

I have tried a lot of different things but I can't get the result I want. The closest I have come is when I drag the mouse along the X axis the box will rotate. It's almost what I want and I can work with that. But as a last resort of getting it exactly as I want I post my problem here.

This is what my latest attempt looks like:

 void Update () {
     if (Input.GetMouseButton (0)) {
         if(isRotating == false)
         {
             isRotating = true;
         }

         if(isRotating == true)
         {
             Rotate();
         }
     }
 }

 void Rotate()
 {
     mousePos = Input.mousePosition;
     
     objectPos = _selectObject.selectedObject.transform.position;
     
     Vector3 direction = mousePos - objectPos;
     //Vector3 angle = new Vector3( mousePos, objectPos, 0 );
     
     _selectObject.selectedObject.transform.rotation =  Quaternion.Euler (new Vector3(0f,0f, direction.x));
 }

 void OnMouseUp(){
     isRotating = false;
     _selectObject.selectedObject.transform.rotation = rotation;
     Debug.Log("UP");
 }

It almost follows the mouse when circling around. But jumps back and forth a lot.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SunnyChow · Sep 15, 2016 at 05:47 AM

you can get mouse world position by raycasting to the floor with the mouse screen position.

using the mouse world position of current frame and last frame, you can get the rotation change by

         Quaternion.FromToRotation (lastMousePos-cubeWorldPos,nowMousePos-cubeWorldPos);

and add this rotation change to the cube

 cube.transform.rotation = rotationDelta*cube.transform.rotation ;

with this, you should be able to spin the cube with mouse

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 iabulko · Sep 15, 2016 at 05:47 AM

Probably the best method will be using rays. (I commented OnMouseUp() function - didn't know that was that doing ;>, and I'm not using isRotating bool):

 void Update () {
         if (Input.GetMouseButton (0)) {
             Rotate();
         }
     }
     void Rotate()
     {
         Vector3 pos = Vector3.zero;
         //Create a touch plane that is going through object
         Plane groundPlane = new Plane();
         groundPlane.SetNormalAndPosition(Vector3.up, _selectObject.selectedObject.transform.position);
         //Shooting a ray from camera to a point that mouse is pointing, and getting a point from that ray crossing the plane 
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         float rayDistance;
         if (groundPlane.Raycast(ray, out rayDistance))
             pos = ray.GetPoint(rayDistance);
         //Just let the object look at that direction ;)
         _selectObject.selectedObject.transform.LookAt(pos);
     }
     /*void OnMouseUp(){
         isRotating = false;
         selectedObject.transform.rotation = rotation;
         Debug.Log("UP");
     }*/
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Camera rotation around player while following. 6 Answers

RayCasting shows wrong point 1 Answer

Rotation of an object with some delay.. 0 Answers

Multiball Situation in Pong 0 Answers

Make my Gunpoint aim/rotate on the z axis towards my mouse cursor 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