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 ac566 · Sep 20, 2014 at 07:09 AM · rotationinputmouserotatearound

How to rotate an object around a fixed point so it follows the mouse cursor?

I want to rotate the purple rectangle around the red square in reference to the center of the square. In addition I want to be able to have the rectangle follow the mouse cursor wherever it goes. Here is a picture for demonstration:alt text

Here is the code I have already written:

 void FixedUpdate(){
         transform.RotateAround(new Vector3(0.0f, 1.25f, -12.5f), Vector3.up, Vector3.Angle(new Vector3(1f,1f,0f),new Vector3(Input.mousePosition.x,Input.mousePosition.y, 0f)));
     }
 

This code doesn't work and just rotates the rectangle around the square at a fixed pace with no noticeable influence from the mouse. Thank you for your help.

example.png (13.5 kB)
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 ac566 · Sep 20, 2014 at 01:39 PM 0
Share

Here to be more clear. This takes place in unity3d but appears as a 2d game. Y is the direction facing the camera. I want the rectangle to only rotate around the center of the red Square(really a cube) only around the y axis(the rectangle is actually above the cube, so dont worry about them colliding). Both objects will move around with the movement keys at the same time.

1 Reply

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

Answer by robertbu · Sep 20, 2014 at 07:30 AM

Here is one solution. If the center object does not move, you can move the calculation of 'centerScreenPos' to Start().

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour {    
 
     public Transform center;
     private Vector3 v;
 
 
     void Start() {
         // Requires the block to be directly to the right of the center
         //   with rotation set correctly on start
         v = (transform.position - center.position);
     }
     
     void Update(){
         Vector3 centerScreenPos = Camera.main.WorldToScreenPoint (center.position);
         Vector3 dir = Input.mousePosition - centerScreenPos;
         float angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
         Quaternion q = Quaternion.AngleAxis (angle, Vector3.forward);
         transform.position = center.position + q * v;
         transform.rotation = q;
     }
 }
Comment
Add comment · Show 6 · 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 ac566 · Sep 20, 2014 at 01:41 PM 0
Share

Sorry this doesn't work for me. The rectangle it seems moves around the x axis and I want to only rotate it along the y axis relative to a point. I made another comment to my original post that hopefully will clarify my intent better.

avatar image robertbu · Sep 20, 2014 at 01:53 PM 0
Share

For the camera looking down the 'y' axis, change line 20 to:

  Quaternion q = Quaternion.AngleAxis (angle, Vector3.down);
avatar image ac566 · Sep 20, 2014 at 02:19 PM 0
Share

Thank you! It seems to be almost working now. $$anonymous$$y only problem is that it is not pointing to where the mouse is exactly, there seems to be an offset. Any ideas on what I'm doing wrong?

avatar image ac566 · Sep 20, 2014 at 02:34 PM 0
Share

Never$$anonymous$$d I got it to work. Thank you so much!

avatar image PRninja8488 · Aug 19, 2015 at 02:16 AM 0
Share

$$anonymous$$ay I ask what you did to get the offset fixed? This is precisely the answer that I needed as well, and I have run into the same issue. I got my object rotating beautifully, but it does not follow the mouse. It seems to be offset by 90 degrees and reversed.

EDIT: I totally got the word "beautifully" mixed up in my brain with the Pokemon Beautifly...I own it.

avatar image AntonLaverSpil PRninja8488 · Nov 11, 2020 at 07:12 PM 0
Share

@PRninja8488 uber late answer but for my fix with the offset i just had to zero out every position other than x which is on 2 :)

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

RotateAround in local coordinates? 1 Answer

Need help on the 3ds max style camera control 0 Answers

Keep objects current rotation, with Input Mouse X 0 Answers

How to rotate an object around another facing to mouse? 1 Answer

Rotating object with a mouse movement 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