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 sanks007 · Oct 23, 2013 at 06:35 AM · rotationtransform.rotationtransform.rotatetransform.rotatearound

Rotating game Object.

I know this questions have been asked multiple times and many solutions have been given.And some are working according to my requirements though in different example,but not in the actual project that i am working on. The problem is i want to rotate a gameobject around its center. The game Object here is not a simple one though. It may look something like ![this ][1] or like ![this][2] or may be some other form . All the component you see are different objects and as and when they are coming on the screen they are made child of this gameobject. So in the first case main game Object is named as singleLine. So all the components on screen are made child of singleLine. Now the position of singleLine would be the center of E-2. I am not been able to rotate it around the exact center of E-2 rather it moves in a circular manner. How to do this. I am posting a script out here of all the things that i have tried.

 var targetItem : GameObject;
     var rotationRate : float = 1.0;
     var hit: RaycastHit;
     public var background:GameObject;
     public var cam:Camera;
     
     var smooth = 1.0;
     var tiltAngle = 30.0;
     
     function Update()
     {
         if (Input.touchCount > 0) 
         {
              var theTouch : Touch = Input.GetTouch(0);
              var ray = cam.ScreenPointToRay(theTouch.position);
              if(Physics.Raycast(ray,hit))
              {
                  if(hit.collider==background.collider)
                  {    
                      if(Input.touchCount == 1)
                     {
                             if (theTouch.phase == TouchPhase.Moved) 
                            {
                                //this code is for x-axis rotation
                                if(Input.GetTouch(0).deltaPosition.y < 0|| Input.GetTouch(0).deltaPosition.y > 0)
                             {
                                 var tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
                                 var target = Quaternion.Euler (tiltAroundX, 0, 0);
                                 targetItem.transform.rotation = Quaternion.Slerp(targetItem.transform.rotation, target,Time.deltaTime * smooth);targetItem.transform.Rotate(0,theTouch.deltaPosition.x * rotationRate,0,Space.World);
                                 targetItem.transform.Rotate(theTouch.deltaPosition.x * rotationRate,0,0,Space.World);
                             }
                                if(Input.GetTouch(0).deltaPosition.x < 0  || Input.GetTouch(0).deltaPosition.x > 0)
                             {
                                 var tiltAroundY = Input.GetAxis("Horizontal") * tiltAngle;
                                 var target1 = Quaternion.Euler (tiltAroundY, 0, 0);
                                 targetItem.transform.rotation = Quaternion.Slerp(targetItem.transform.rotation, target1,Time.deltaTime * smooth);
                                 targetItem.transform.Rotate(0,theTouch.deltaPosition.y * rotationRate,0,Space.World);
                             }
                             wasRotating = true;
                         }    
                      }
                  }    
             }
         }
     }

Have tried using only targetItem.transform.Rotate(0,theTouch.deltaPosition.y * rotationRate,0,Space.World); as well but again not helping out . Tried targetItem.transform.rotation(targetItem.transform.position,Vector3.down,20*Time.maximumDeltaTime); as well but again similar result.What could be the reason for it. Please help me. [1]: /storage/temp/16892-firstlayout.png [2]: /storage/temp/16893-secondlayout.png

secondlayout.png (19.7 kB)
firstlayout.png (15.5 kB)
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
1

Answer by Tomer-Barkan · Oct 23, 2013 at 08:15 AM

Make an empty GameObject, call it center, and locate in the exact center of the object (the exact point that you want the rest of the components to rotate around).

Then make all the other components children of the center game object.

In your code, add a reference to the center object, and then it's very simple to rotate everything around it:

 center.transform.Rotate(xAngle, yAngle, zAngle);
Comment
Add comment · Show 12 · 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 sanks007 · Oct 23, 2013 at 08:40 AM 0
Share

well that's what i am doing. Created an empty GameObject 'center' by using transform.parentand made all the other element its child. The problem i see is that as the gameobjects increases the 'center' get shifts to the center of the entire game Object.I would say the same script is working as gem in test demo. In which all the game objects are created in editor and not at run time.

avatar image Tomer-Barkan · Oct 23, 2013 at 08:49 AM 0
Share

I didn't see any object creation in your code, I thought you created them in the editor... share that part of the code please. If you do create them in the editor, share the object hierarchy.

avatar image sanks007 · Oct 23, 2013 at 08:52 AM 0
Share
 `cylinderbond.transform.parent=GameObject.Find("RotateElementCenter").transform;` 

and this is code for creating the connection between the two spheres sphereElement.transform.parent=GameObject.Find("RotateElementCenter").transform; for spheres. And for hierarchy its something like this ::

RotateElementCenter 1. Element1 2. Element2 3. Cylinder1 and so on..

avatar image Tomer-Barkan · Oct 23, 2013 at 09:05 AM 0
Share

The RotateElementCenter position should not change when you attach new children to it. You can check in the inspector while running your game.

There must be something else going on that's interfering.

I tried to recreate a scenario, had a empte center objet, and two cubes located around it but not attached as children.

Then I had a script that would attach both children to the parent:

 public class Test2 : $$anonymous$$onoBehaviour {
     void Start () {
         transform.parent = GameObject.Find("Center").transform;
     }
 }

And another one that would rotate the center object:

 public class Rotate : $$anonymous$$onoBehaviour {
     void Update () {
         transform.Rotate(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z + Time.deltaTime * 0.0001f);
     }
 }

And it works just fine. So it's not a problem with that part of the code, there might be other factors in your game that are affecting this.

avatar image sanks007 · Oct 23, 2013 at 09:07 AM 0
Share

$$anonymous$$ay be some other problem I take that . I will check it in the demo i created and see if that is working fine..! will get back to you .. !

Show more comments
avatar image
0

Answer by yogee · Oct 23, 2013 at 09:07 AM

   for (var i : Transform in singleLine)
         {          
             allPoint+= i.position;
         }      
 
         var groupCenter = allPoint/ singleLine.childCount;
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 sanks007 · Oct 23, 2013 at 09:09 AM 0
Share

what are groupCenter and groupVectors variables.

avatar image yogee · Oct 23, 2013 at 09:13 AM 0
Share

now the code will work

avatar image sanks007 · Oct 23, 2013 at 09:19 AM 0
Share

allPoint is variable of type transform or Vector3

avatar image yogee · Oct 24, 2013 at 04:23 AM 0
Share

it,s a vector man, i.e, WholeObjCenter= Sum of all objs position / no. of objs(child)

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

16 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

Related Questions

Transform.RotateAround Issues 1 Answer

How to rotate object around another object with less than 1 degree. 0 Answers

Transform.Rotate() is rotating less than given rotation. What's going on? 4 Answers

How to add limit to object rotation? 1 Answer

Have Head Bone Rotate with Camera 2 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