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 kelebra · Apr 30, 2013 at 01:24 PM · rotatearoundrotatearoundpivot

Rotate object around other object (or circle) using mouse

Hi everyone,

As the title suggests I'm trying to rotate an object around another object using the mouse. I've managed to rotate a cube around a capsule with this code :

     public float PlanetRotateSpeed = -0.005f;
     public float OrbitSpeed = 0.0005f;
     protected GameObject capsule;
     
     void Start () {
         capsule = GameObject.Find("capsule"); 
     }
         
     void Update () {
         
             transform.RotateAround (capsule.transform.position, Vector3.forward, OrbitSpeed* Time.deltaTime);
     }

So the cube rotates around the capsule as if it's moving along a circle (or sphere), what I want to do (and can't actually do) is keep the cube moving along the circle but not with "OrbitSpeed* Time.deltaTime" but depending on the mouse position.

alt text

Thanks in advance.

unityanswers.png (27.9 kB)
Comment
Add comment · Show 3
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 robertbu · May 02, 2013 at 04:04 PM 0
Share

I can think of half a dozen different ways you can approach this problem, but which one will depend on a number of different factors. What is the relationship of the camera to the objects (i.e. will it be looking down the Z axis for example)? Will the object being rotated around be moving in the scene? Will the rotation always be parallel to the plane of the camera?

avatar image kelebra · May 03, 2013 at 09:06 AM 0
Share

Hey, I realize I should have been more precise with my questions, so to answer yours : It's a 2D game, we use the X & Y axis, the object does move in the scene (it's attached to the player), and yes the rotation will always be parallel to the plane of the camera, I've tried with the $$anonymous$$ouseOrbit script but I'm not really liking the end result.

avatar image JK_Dev · Aug 24, 2015 at 01:33 PM 0
Share

is there a way of adding a speed variable on the second method?

1 Reply

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

Answer by robertbu · May 03, 2013 at 01:29 PM

As mentioned in my comment, there are multiple different ways of achieving this behavior, but none are simple. It is easier as a 2D game looking down the Z axis.

 using UnityEngine;
 using System.Collections;
 
 public class Around4 : MonoBehaviour {
     
     public Transform target;
     public float fRadius = 3.0f;
     private Transform pivot;
 
     void Start() {
         pivot = new GameObject().transform;
         transform.parent = pivot;
     }
     
     void Update () {
         Vector3 v3Pos = Camera.main.WorldToScreenPoint (target.position);
         v3Pos = Input.mousePosition - v3Pos;
         float angle = Mathf.Atan2 (v3Pos.y, v3Pos.x) * Mathf.Rad2Deg;
         
         pivot.position = target.position;
         pivot.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
     }
 }

Here is a second way that positions the object on the circle. The object is not rotated, so you would need to add a LookAt() if the object needs to be rotated:

 using UnityEngine;
 using System.Collections;
 
 public class Around2 : MonoBehaviour {
     
     public Transform target; 
     public float fRadius = 3.0f;
     
     void Update () {
         Vector3 v3Pos = Camera.main.WorldToScreenPoint (target.position);
         v3Pos = Input.mousePosition - v3Pos;
         float angle = Mathf.Atan2 (v3Pos.y, v3Pos.x) * Mathf.Rad2Deg;
         v3Pos = Quaternion.AngleAxis (angle, Vector3.forward) * (Vector3.right * fRadius);
         transform.position = target.position + v3Pos;
     }
 }




Comment
Add comment · Show 2 · 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 rtoselli · Feb 24, 2014 at 09:11 AM 0
Share

I can't Upvote yet, but i successfuly used your snippet to accomplish the desired behavior of this question, thanks!

If possible, can you elaborate on the math of this solution?

avatar image robertbu · Feb 24, 2014 at 04:30 PM 0
Share

If possible, can you elaborate on the math of this solution?

Are you struggling with something specific? In the first solution I do:

  • Line 16 - I convert the position of the object to a screen position. This places the mouse and the object in the same coordinate system.

  • Line 17 - Subtract the v3Pos from the mouse position gives a vector from the object to the mouse...the object becomes the center of the 'world'.

  • Line 18 - We get the angle of this vector where Vector3.Right would be 0.0 degrees.

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

14 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Orbit 2D camera around circle but keep lookat towards z axis 1 Answer

Rotating a Transform around another Transform on the X, Y, and Z Axis 1 Answer

Creating a 3D orbital movement 0 Answers

GUI rotation not working 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