- Home /
Rotating Object around local Z axis over given time and degrees and stacking if triggered before finish
Hi there, i have an object, a revolver magazine, i want to rotate it arround its local Z axis (gun itself moves freely on all axis) about 60 degrees over 0.6 seconds when an event is triggered. The complicated part is, if the event gets triggered again before the rotation is finished, i want to add the ammount and time (stacking). Lets say it is half way done, and it gets triggered again, it should rotate 90 degrees over 0.9 seconds, from that moment on. And the rotation speed shoulnt change (always same).
Answer by Commoble · Apr 21, 2017 at 10:55 PM
This isn't as complicated as it sounds; consider it like this:
-You're applying an affect to your object.
-The effect does this: Add a particular amount of degrees to rotate over a period of time.
-The angular velocity (degrees per second) is constant, so whatever adds this effect to the object only needs to know how large of a rotation to add to it, or how long to rotate it for; one can be calculated from the other (reminder: degrees per second = degrees / seconds, and from this we get seconds = degrees / dps, and degrees = seconds * dps).
-Adding the effect to the object while it's already rotating adds an additional amount of rotation to rotate through, without resetting the rotation or changing the speed.
Finally, to bring all of this together, we think of how the Update event works. The Update is called each frame (which can be somewhere around 60 FPS if there's no performance issues), so for something that happens over a period of time, we can perform a small fragment of that thing each frame. Time.deltaTime tells us how long the previous frame lasted, and so we use this to determine how much of our task to do in a single frame.
We'll probably also need a way for other scripts to tell your object to rotate. We'll make a public AddRotation (float degrees) function that can be called from any script with a reference to this object. With all this in mind, you can add something like the following to your object's script:
protected float degreesRemaining = 0F;
public const float degreesPerSecond = 100F;
public void AddRotation(float degrees)
{
this.degreesRemaining += degrees;
}
void Update()
{
if (this.degreesRemaining > 0F)
{
// how much the object should rotate this frame
// degrees = degrees per second * seconds, and Time.deltaTime is a fraction of a second
float rotationThisFrame = degreesPerSecond * Time.deltaTime;
// if object would finish rotating this frame, finish rotating
if (rotationThisFrame >= degreesRemaining)
{
this.transform.Rotate(new Vector3(0F, 0F, degreesRemaining);
this.degreesRemaining = 0F;
}
else // otherwise, rotate the amount necessary and subtract that from the counter
{
this.transform.Rotate(new Vector3(0F, 0F, rotationThisFrame);
this.degreesRemaining -= rotationThisFrame;
}
}
}
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Random Rotation To Animation HELP! 2 Answers
How to limit sprite rotation 0 Answers
Clamping objects localrotation not working (C#) 2 Answers
can't get local rotation of object 2 Answers