• 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
11
Question by torg · Dec 03, 2009 at 12:19 PM · rotationgameobject

How do I rotate and object around its center in script?

I want to rotate an object around its center point. I've tried RotateAround, and what seems like every possible way to use both Rotate and transform.rotate.

To give some more detail my object is an empty gameObject containing several children, I'm translating my parent object to a specific position and doing two rotations, one Vector.right * 90 (or Rotate(90, 0, 0) and a random one (0-360) along what after the first rotation is the y-axis. If I do the first rotation in the script and proceed to rotate around the y-axis in the editor I get the result I'm after namely that it rotates around its own center point. In the script however it seems to rotate around the center pivot of the first child of the object I'm rotating. Of note it seems like the script is doing the rotation without changing the objects position, while the editor reflects a new positions for x and z while I'm rotating around y.

My original code was this: objectName.Rotate(90, Random.Range(0, 360), 0); But as stated I've tried doing the two rotations separately and also using RotateAround with a the objects positions as the rotation point. (the objectName var refers directly to the objects transform)

Comment
Add comment · Show 4
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 duck ♦♦ · Dec 03, 2009 at 12:29 PM 0
Share

could you include the code that you're trying? the basic "transform.Rotate()" function should rotate an object around its own centre point, so if you're calling this on the empty parent, the children should all get rotated around it.

avatar image Novodantis 1 · Dec 03, 2009 at 12:47 PM 0
Share

It may also be worth checking the coordinates of the children in the Inspector window, as they will be relative to the parent (ie EmptyGameObject). The little trasform gizmo appears to be drawn in the approximate centre of the object and children collectively, not at the parent's origin as in other programs like 3D $$anonymous$$ax.

avatar image torg · Dec 03, 2009 at 12:57 PM 0
Share

Is there anyway to set the actual pivot or center of the parent gameObject? Looking at it in the scene view, the pivot (or transform gizmo) is at the approximate center of the object, which I've made sure of by moving it along the x-axis to make up for the individual positions of the children. This is why I cant understand that the rotation does not respect this center point in the script, but clearly does when the rotation is performed in the editor.

avatar image cokefenta · Mar 19, 2015 at 12:20 PM 0
Share

now ,i have a question that about the "rota$$anonymous$$round".today,i find that the "rota$$anonymous$$round" can't be used. i only can use the rotate. but i want to make one thing rotate the other thing.how can i use the "rotate" to make it ?

7 Replies

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

Answer by runevision · Dec 04, 2009 at 10:20 AM

When you modify the rotation of a Transform, the rotation happens around the local origin - that is, the center of the local coordinate system for the object. The actual middle of the object may not be placed at the origin - that depends on how it was modeled.

To make the rotation around the actual middle, there are a few things you could do.

  • Rotate around the bounds center. You can use collider.bounds.center to get the center of the world space bounding box of the collider of the game object, get the center of the local space bounding box of the mesh. In both cases, you can then use RotateAround to rotate around that point. Note that for the mesh bounds, you need to transform the center into global coordinates first.

  • Make an empty parent game object to the game object you want to rotate. Place the game object such that its middle is placed right at the origin of the parent. You can then rotate the parent game object instead using the normal ways to set the rotation of the transform.

Comment
Add comment · Show 4 · 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 cregox · Jun 04, 2012 at 07:45 PM 0
Share

There is at least 1 case ( http://answers.unity3d.com/questions/137688/pretty-weird-issue-with-rotating-a-sphere.html ) in which neither of these work (when importing a messed up mesh with pivot not in center), and they both look messy. Is there any "cleaner" solution nowadays? (years after this answer)

avatar image cregox · Jun 04, 2012 at 10:00 PM 1
Share

Still keeping an interest in the question above (is there a more pleasant solution), I need to fix my mistake: using bounds do work, but we need to use RotateAround and apparently nothing else (such as RotateAroundLocal). Also, it's simpler to get renderer.bounds.center rather than collider, as @wolfram suggested me elsewhere.

avatar image cregox · Jun 11, 2012 at 08:01 PM 1
Share

Here's the answer to my questions: use a new parent GameObject translated into the now child's renderer.bounds.center - which is basically a combination of what you said: http://answers.unity3d.com/questions/137688/pretty-weird-issue-with-rotating-a-sphere.html#comment-263630

avatar image $$anonymous$$ · Aug 27, 2014 at 02:26 AM 0
Share

It says RotateAround is deprecated.

avatar image
10

Answer by Pixelblock Games · Feb 18, 2013 at 03:22 AM

Rotating objects in the Unity3d Engine is extremely easy. For instance, I use the following code to rotate an object in the scene 360 degrees:

function Update () {

      transform.Rotate(0,20*Time.deltaTime,0);

}

Just create a new JavaScript, and paste this inside the script!

Comment
Add comment · Show 7 · 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 grojguy · Mar 23, 2013 at 04:09 PM 3
Share

@Pixelblock Games - I usually like to read the entire thread before posting an 'answer'. In doing so, it helped me see that torg is presenting a considerably more-complex issue, which your answer does not help with at all.

avatar image Safvan7 · Sep 02, 2013 at 08:22 AM 0
Share

i want to rotate the object manually by touch in android devices, how do i do that? can someone give me the script for it?

avatar image geomorillo · Sep 09, 2013 at 04:13 AM 0
Share

this answer does rotate 360 degrees around y axis but it does not rotate in the center of the object

avatar image crare geomorillo · Jan 13, 2017 at 03:57 PM 0
Share

check your gameobjects origin point is in middle of the gameobject. If you imported it from Blender you can do it there. In Unity editor you can see the origin point if you bring the gameobject to the scene view and select the cursor which lets you move it in 3 axis. Origin point is the point from where those axis-arrows go outwards.

avatar image Bunny83 crare · Jan 13, 2017 at 05:03 PM 1
Share

The handle is not necessarily at the objects pivot / origin. Only when you switch Unity into pivot mode.

If you are on "center" mode the handle will always be at the objects center.

Show more comments
avatar image Ziplock9000 · Oct 17, 2018 at 08:00 PM 0
Share

This rotates around the pivot point, not necessarily the center as the OP asked.

avatar image
4

Answer by IndustrialCode · Sep 01, 2014 at 06:27 AM

Under the "GameObject" menu in Unity, there is now a command called "Center On Children". This will move the pivot point of the object to the midpoint of the total bounds of all your children. After this, you can use any method to spin around an axis and it will behave just like you want it to. :)

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 giantkilleroverunity3d · Nov 12, 2019 at 08:06 PM 0
Share

'Center on Children' solves a huge amount of hierarchial problems! This option is a nesting God send!

avatar image umerrazzaq333 · May 10 at 12:28 PM 0
Share

Thanks You solve my problem

avatar image
3

Answer by h2hjastermereel · Dec 03, 2009 at 03:09 PM

Like Ducky stated, we would need to see your code. I am wondering where you are updating the rotation and how. Its one thing to call transform.Rotate but where are you doing this and how often?

For instance this works for me:

using System; using UnityEngine;

[AddComponentMenu("Scripts/Quests/SpinningSymbol")] public class SpinningSymbol : MonoBehaviour { private void Update() { transform.Rotate(Vector3.forward Time.deltaTime 100); } }

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
1

Answer by Bampf · Dec 04, 2009 at 02:22 PM

It sounds like you might need to give some thought to the way you compose consecutive rotations. By default, the Transform.Rotate method always rotates using the object's local coordinate system. This system does not change when you move or rotate the object. So if you rotate the object 90 degrees around Vector3.right, then what you might think of as the Y-axis in the scene/editor is the local Z-axis. (Note: in recent versions of the Unity editor you can choose whether the tool handles are displayed using local coords or not. Select your object, and click on the button marked "Global" or "Local" above the Scene View to switch from one to the other.)

If you want to rotate around the world's "up" direction, regardless of the object's local coordinate system, you can try adding Space.World as the last argument to Rotate.

Alternatively you can use only local coordinates for your work. You will need to rotate around the local axis that represents Up from the object's point of view, regardless of how its current orientation in the world. (Again, you can tell the Scene editor to display the rotation/translation tool handles in the local coordinate system to help keep this straight.)

One additional comment: if the first 90 degree rotation is being done to correct an orientation problem with the model, you might consider doing that in the modeller instead. By defining the correct "up" and object center in the modeller, you make working with it in Unity a bit more intuitive.

(I suppose another way around this might be to rotate the object 90 degrees, then parent it to a new game object whose local coordinate system is more to your liking. This is not something I've tried though.)

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
  • 1
  • 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

17 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 avatar image avatar image avatar image

Related Questions

C# Rotate GameObjects Regardless of List Size 2 Answers

C# GameObject Reverses Z Rotation 0 Answers

Using the accelerometer to rotate Camera 1 Answer

How to tilt the gameobject based of Input.Acceleration? 0 Answers

How to script a forever going swing? 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