- Home /
Problem rotating spheres around empty parent
I am working on a 2d sidescrolling game.
I am trying to implement a "shield" for the player's ship made up of 4 spheres. These 4 spheres are set as children of an empty parent object I have attached to my ship. My goal is to have them spin around the player ship's Z axis.
I figured I could create the empty parent and set it on my ship's center, add the 4 spheres to the empty object as children, zero out their locations and then move them in their respective directions to achieve a north south east west orientation of 4 spheres spaced 3 units away from the center. I then created a script and attached it to the empty parent object which would rotate the parent 10 degrees a second.
Here is my script, very simple but maybe I'm using rotate wrong. I will explain below.
#pragma strict
var myShieldTransform : Transform;
function Start () {
myShieldTransform = gameObject.transform;
}
function Update () {
//Rotate the object around its Z axis at 10 degree/second.
myShieldTransform.Rotate(0, 0, (10 * Time.deltaTime));
}
The script will rotate all 4 spheres as I expected, however, they become skewed compared to their original orientation while moving around. I'm kind of stumped as to why. I have tried making sure my ship, empty parent, and all spheres are at 0 in the Z axis. The sphere size and scale does not change while running, but the collider's green wireframe does not stay consistent with the sphere's borders. I have attached an image to clarify.
Thank you for any help. I don't want to proceed until I figure this out. When I fully implement this the shield not having matching colliders will look funny when deflecting enemy fire.
You said it is a 2D game, if they are being skewed try to forcefully align it in the axis you are not using to keep it as much aligned as possible, if the problem persist then come back.
Hello Robert.
Thank you for your input. I have "unparented" my shield. When it is not attached to my player's ship the colliders stay consistent throughout the rotation. You're the man! When I use this code no problems arise.
#pragma strict
var myShieldTransform : Transform;
var shieldPivotPoint : GameObject;
function Start () {
myShieldTransform = gameObject.transform;
shieldPivotPoint = GameObject.FindGameObjectWithTag("shieldpivot");
}
function Update () {
myShieldTransform.transform.position = shieldPivotPoint.transform.position;
// Slowly rotate the object around its Z axis at 10 degree/second.
myShieldTransform.Rotate(0, 0, (10 * Time.deltaTime));
}
Thank you for your help but I am still curious as to why this happens. I can send you any script you would like or perhaps the project. I am a student developing for my portfolio so I see no reason to be secretive of my code.
Is there anyway to give you credit for answering this in a comment?
I converted my comment to an answer. If you click the checkmark on the left of the answer, it will close the question out and give me a bit of $$anonymous$$aram.
Now that you know it is the parent/child relationship between the two that is causing the problem, go back and look at any rotation or scaling that is going between the root object (ship) and pivot point. This includes both local and global rotations in any of the object that would have been higher up the parent/child chain. I suspected when I posted my original suggestion that somewhere in that chain something is rotating, though given this is a 2D game, I couldn't see why would want to. You can also run the game and look at the rotations in the Inspector of all the objects.
Answer by robertbu · Apr 02, 2013 at 04:13 PM
I like figuring out rotation problems, but from the information given, I cannot figure out how you are getting this behavior. Here is something to explore. Make the four spheres a child of the empty game object as you've done here, but don't make it a child of the ship. Next an empty game object as a child of the ship at the pivot point. Now in the script above, have it track the pivot object. That is, your shield will follow the ship but it will not be a child of the ship. This may or may not fix the problem, but it will tell you something about the nature of the problem.
Your answer
Follow this Question
Related Questions
How do I rotate a Collider? 3 Answers
Rigidbodies not reacting to one another 2 Answers
Make a capsule collider BETWEEN 2 objects 0 Answers
What is the simplest way to get a sphere to rotate as it moves in game? 1 Answer
Quick Shortcuts for Rotation 0 Answers