- Home /
RotateAround x degrees over y time
At the moment I have the following code:
if (Input.GetKeyDown(KeyCode.A)) {
transform.RotateAround (Vector3.zero, transform.up, 5 * Time.deltaTime);
}
This rotates the object as I want to along the surface of my sphere however I'd like to make it do this 5 degree rotation over y seconds on button press. The most obvious way that I can think of is to have a timer variable however there must be a less convoluted way to rotate the object and have it stop exactly at 5 degrees. I'd appreciate any help - please let me know if more information is required.
Does the object rotating ever change it's 'y' rotation? That is, you are using transfor.up of the object that is rotating which is strange. $$anonymous$$aybe you want Vector3.up? And what should happen if the user presses the button a second time before 'y' times is over? And will the object always rotate around Vector3.zero?
The object sits on a sphere that is constantly rotating about its own axis. The object in question will move along the surface of the sphere with this code (I actually believe you helped me with my last issue as well for this: http://answers.unity3d.com/questions/481661/why-am-i-getting-2-different-results-with-rota$$anonymous$$r.html#comment-481901)
If the user presses the button again nothing should happen - I'd like the "animation" to complete before being able to accept the input again. The object will always rotate around 0,0,0 which is the center of the sphere.
The requirement to lock out the key while the rotation takes place plus, based on your previous question, you want to rotate around an arbitrary axis (rather than x,y or z) eli$$anonymous$$ates several simple answers for this question. A timer seems like a reasonable solution. There are ways of doing it without a timer, but they would be complex to understand and maintain.
Ins$$anonymous$$d of locking the key would it be better if we just added 5 degrees to the target angle every button press? What do you mean by arbitrary axis? I though the axis I am rotating around is based on the objects orientation on the sphere. Could you let me know what the names for the more complex solutions are so I can at least research them?
Answer by robertbu · Jun 29, 2013 at 05:49 AM
I'm not completely sure of your requirements. Making some assumptions, here is how I might code it if I did not want to use a timer:
#pragma strict
private var sphere : Transform;
private var trCenter : Transform;
private var qTo : Quaternion;
var speed = 5.0;
function Start(){
sphere = GameObject.Find("Sphere").transform;
trCenter = new GameObject().transform;
trCenter.rotation = sphere.rotation;
trCenter.parent = sphere;
trCenter.position = sphere.position;
transform.parent = trCenter;
qTo = transform.localRotation;
}
function Update() {
if (Input.GetKeyDown(KeyCode.H)) {
var axis = Vector3.Cross(transform.right, transform.position - sphere.transform.position);
var q = Quaternion.AngleAxis(5.0, axis);
qTo = q * qTo;
}
trCenter.localRotation = Quaternion.RotateTowards(trCenter.localRotation, qTo, speed * Time.deltaTime);
}
What it does is create a game object and place it at the origin. The physical game object that you want to rotate around the sphere is then made a child of the empty game object. Quaternion.RotateTowards() rotates at a specified speed. In this case I set the seed to 5.0 which means a 5 degree rotation will take 1 second. If you wanted it to take 1/2 second, you could set speed to 10.0. Or if you really wanted to specify time, you could create a time variable and set speed to '5.0 / time' for a five degree rotation. Rotation is applied to the empty game object at the origin, and the physical game object as a child goes along for the ride. To me this is not simpler than using a timer, and it would take some additional code to prevent the keypress during rotation.
This helps my understanding a lot. The only issue, however, is that the cubes that spawn (probably a decent number of them) should be children of the sphere because the sphere is constantly rotating and I want the cubes to rotate with it along the spheres axis. What I was thinking of doing was the following:
-Sphere
--Empty GO
---cube
---cube
---...
With this I assume the empty GO will rotate with the sphere and, thusly, so will the cubes. Since all the cubes spawn as children of the GO then I think I can use your code to apply the rotation to the GO without affecting the rotation of the sphere.
So now I thought of having the GO as part of the scene and set to be a child of the sphere. Now the GO will spawn the cubes and set itself as their parent. Then, on button press, the GO will rotate over time but also reflect the constant rotation of the sphere on the cubes.
When I try this now (with the script on the GO at 0,0,0 with rotate function) 2 things happen. The cubes turn off their axis (I assume because of the sphere's rotation) and the cubes also don't rotate as quickly as the sphere for some reason.
I want to make sure I understand. You want the cubes to rotate around the axis of the sphere. As children they will rotate with the sphere. But the code above will advance the rotation around the axis? Do I have this right?
I've attached a poorly drawn picture. the top 2 pictures show the rotation of the sphere and the cube and then the bottom show where the cube should move to on button press. The code shouldnt advance the cubes along the same axis but move them along an almost perpendicular (tangential?)one.
The picture did not help me at all. I can see several ways in my head how you might want this work. I made some modifications in the code above to implement one way. Note it expect the central sphere to be the one and only game object named "Sphere". Change the name to match whatever you named your central sphere.
Here is a screenshot from the project that should help illustrate what I'm trying to do. The sphere is rotating in a way so that the cubes that spawn at 0,0,1 go towards the top (green cube). I'd like to be able to press a button and have all of the red cubes move to their left or right but keep the same orientation. As you see the cubes form lanes that are 5 degrees apart from each other. When we move the red cubes they should end up in one of these lanes. With your solution the cubes end up going in very different directions for some reason. I assume that this is because of the rotation of the sphere again.
In the future I'd like to be able to move individual red cubes as well ins$$anonymous$$d of all of them as a whole. I'll be moving them farther along the sphere forwards or backwards.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105059im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why am I getting 2 different results with RotateAround? Trying to move cubes on surface of sphere. 0 Answers
How to Rotate Player Around Specific Angle. 0 Answers
creating a timed function 3 Answers
Unity how to find load time? 1 Answer
How do I rotate an object around another to have its opposite ? 0 Answers