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
1
Question by Pakarat · May 24, 2014 at 09:00 PM · rotate objectrelative rotation

Rotate object via script like in editor

Hello, I currently have trouble rotating object(with all parented objects). I want same rotation like in editor, just using script. I want to set rotation on y=90, but when I do the object position change, but not in the inspector(just visual). And that's my problem, so I just want to change rotation, and object should stay in place.

Like this: http://videobam.com/VgKrn As you can see when I rotate in editor the rotation and position change. But via script just rotation change, moving object away from its original position.

Note: The object I want to rotate, I created using script, and then I attached some cubes as parent.

-Thanks

Comment
Add comment · Show 2
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 24, 2014 at 09:28 PM 0
Share

There's not enough information here. If you create the object in code, where do you define the pivot point? Are you trying to rotate around the center of the individual object or around the center of the collection of objects? Or rotating the parent?

avatar image Pakarat · May 25, 2014 at 06:03 AM 0
Share

When I create that object via script and set parent. The object position is where it should be(in center of parented objects). But object position is(0,0,0), and I can see that the object position is not (0,0,0). Then when I rotate, I think it rotates around position(0,0,0). I'm trying to rotate around the center of the collection of objects. Also I never define object pivot.I just create gamobject,then create few cubes, then parent them to gameobject.

1 Reply

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

Answer by robertbu · May 25, 2014 at 06:27 AM

First, the object position reported in the Inspector is the local position. It is relative to the parent. So you will only get a position of (0,0,0), if the object is in the same position as the parent.

There are a few things I'm still fuzzy about in your questions. Here is a guess at some sample code. It uses a Bounds to calculate the center point of all the game objects based on the pivot point of all the objects in the collection. It will work well if all the objects are approximately the same size and there are no empty game objects in the collection. If you have objects of significantly different sizes, or if you have empty game object in your parented/childed set of objects, then you will need to do some additional work.

Start with a new scene. Build a collection of objects sharing parent/child relationships. Put this script on any object in the collection.

 #pragma strict
 
 var pivot : Vector3;
 var speed : float = 45.0;
 
 function Start() {
     var bounds : Bounds;
     bounds.center = transform.position;
     
     var transforms = transform.root.GetComponentsInChildren(Transform);
     for (var trans : Transform in transforms) {
         bounds.Encapsulate(trans.position);
     }
     pivot = bounds.center;
 }
 
 function Update() {
     if (Input.GetKey(KeyCode.LeftArrow)) {
         transform.root.RotateAround(pivot, Vector3.up, Time.deltaTime * speed);
     }
     if (Input.GetKey(KeyCode.RightArrow)) {
         transform.root.RotateAround(pivot, Vector3.down, Time.deltaTime * speed);
     }
     if (Input.GetKey(KeyCode.UpArrow)) {
         transform.root.RotateAround(pivot, Vector3.right, Time.deltaTime * speed);
     }
     if (Input.GetKey(KeyCode.DownArrow)) {
         transform.root.RotateAround(pivot, Vector3.left, Time.deltaTime * speed);
     }
 }
Comment
Add comment · Show 1 · 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 Pakarat · May 25, 2014 at 06:32 AM 0
Share

Thanks for your help. It worked.

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

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

Related Questions

Rotating sprite towards another sprite 1 Answer

Rotate & Translate 1 Answer

Play movement and restrictions - Similar to a window wiper 2 Answers

I drag a rotated object to a parent - and it has totally different rotation. 1 Answer

Lerp Rotation broken 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