- Home /
Rotating GameObject problem
Hey! I'm making a game for Android and i want to make coins for it. Now i have a problem. I want to have 2 cubes spinning inside a cube but they don't rotate like i want. I made a big Over-Object, that holds them together: coin 1 2 3
I want all together to rotate around the y axis and every seperate to rotate around its own z axis. here is how i mean it;D (Pain(t))
No problem, glad you got it working. I have converted your answer to a comment as it was more suited for that. :) I've also upvoted your question since I thought it was pretty clear from the text and image what you were asking. With your new reputation, you should now be able to comment, upvote and mark as answer. Welcome to Unity Answers!
Answer by CHPedersen · Aug 13, 2013 at 11:40 AM
You have to write 2 different scripts: One to rotate the entire system, and one to rotate each individual coin. You could call them RotateCoinScript and RotateCoinSystemScript.
RotateCoinSystemScript would have an Update method that calls this:
float rotationSpeed = 10;
transform.RotateAroundLocal(Vector3.up, Time.deltaTime * rotationSpeed);
And RotateCoinSystemScript would have an Update method that calls this:
float rotationSpeed = 10;
transform.RotateAroundLocal(Vector3.forward, Time.deltaTime * rotationSpeed);
Then you add the RotateCoinScript to each coin (so they each have their own individual instance of it), and then the RotateCoinSystemScript to the parent object (what you call the "Over-Object").
You can adjust the rotation speed as you see fit. Note that Vector3.up is short-hand for the y vector, and Vector3.forward is the z vector. These are globally static properties that always return (0,1,0) and (0,0,1) respectively, but their use becomes local to the objects when using transform.RotateAround*Local* (as opposed to just transform.RotateAround).
Answer by Narv · Aug 13, 2013 at 11:39 AM
ughhhhhhh....
I assume you're trying to do this in code? Since you haven't said what you're doing to solve the issue. Try making an animation for this and attach the .anim files to the coin "parts".
Answer by Joyrider · Aug 13, 2013 at 11:42 AM
Use Transform.RotateAround for the Y (which will make them rotate around an axis in world coordinates (worldaxes don't change if your object moves)
Transform.Rotate for the Z (which will make your object rotate according to its own axes)
Answer by lixpoxx · Aug 13, 2013 at 12:48 PM
I have made one script with the variables rotatez and rotatey.
I tryed Transform.RotateAround but the Console shows this: Assets/scripts/rotator.js(10,25): BCE0023: No appropriate version of 'UnityEngine.Transform.RotateAround' for the argument list '(int, float, int, UnityEngine.Space)' was found.
make sure the arguments you pass are all floats, and not ints.