Hi guys, I have a problem that can’t resolve alone. I had searched on web and made test but I get stucked.
I want to make the camera rotate on it self to do 360 rotation. If I link the method to a key it work while I’m pressing the button. But I would like to automate the process to get a perfect 360 rotation while reached a selected event (let’s say a score target).
This is what I have right now on my camera. For test purpose I’m using a key to start the event
private void Update()
{
if (Input.GetKey(KeyCode.I))
{
BarellLoop();
}
}
private void BarellLoop()
{
//this.transform.Rotate(0, 0, 5f);
//Debug.Log(this.transform.rotation.eulerAngles.y);
//if (this.transform.rotation.eulerAngles.y > 0)
//{
do
{
Debug.Log(this.transform.rotation.eulerAngles.y);
this.transform.Rotate(0, 0, _rotationSpeed * Time.deltaTime);
} while (this.transform.rotation.eulerAngles.y > 0);
//}
}
As you can see I had tried to log the start position, of couse zero. To make a rotation before do do-loop, to use an if-loop to verify that the first rotation has changed the initial rotation. But every time I enable the loop Unity crash.I had tried while-loop, if-loop and do-loop to rotate the camera until a 360 rotation was made but… I had tried to use a LateUpdate, a FixUpdate just to test, but noghing seems to help
I’m sure that is simple, but I can’t figure it out alone.
Unity is crashing because you have an infinite loop. But that’s just the first part of your troubles. You’re trying to do your entire barrel loop inside a single frame! The user won’t even see it happen! You need to space the loop out across multiple frames. Remember that update gets called once every frame. So you want to just rotate a little bit each time update gets called. Try this:
void Update() {
if (Input.GetKey(KeyCode.I)) {
StartBarrelLoop();
}
BarellLoop();
}
void StartBarrelLoop() {
if (degreesLeftToRotate > 0) {
// We're already in the middle of a rotation. Don't start a new one!
return;
}
// We want to do one full rotation, so let's set this to 360
degreesLeftToRotate = 360;
}
private void BarellLoop() {
// If we have any rotation left to do, just rotate a teensy bit!
if (degreesLeftToRotate > 0) {
// How much can we rotate this frame?
float amountToRotateThisFrame = Time.deltaTime * RotationSpeed;
// We don't want to overshoot, so let's clamp to how many degrees we have left
amountToRotateThisFrame = Mathf.Min(amountToRotateThisFrame, degreesLeftToRotate);
// Do the actual rotation
this.transform.Rotate(0, 0, amountToRotateThisFrame);
// Record our progress
degreesLeftToRotate -= amountToRotateThisFrame;
}
}
1 Like
This work! So, let me understand how it this script is working.
So in the Update method you constantly call BarrellLoop methed but it’s not working cause the value of rotation is zero. Simple enough. In the StartBarrelLoop you check again the value to be sure and set the target rotation. Ok. In the BarrelLoop there is something that I couldn’t made because I don’t understand it completely. A new variable that will have as result the moltiplication of RotationSpeed and Time.deltaTime. Ok, this value will change every frame a little as I know. But the clamp line, this is a mistery for my mind in this moment. Correct me if I’m wrong, I need to understand.
Mathf.min will return the min value between ammontToRotateThisFrame (let’s say Time.deltaTime is 10 and rotationSpeed 25 = 250 for example) and degreeLeftToRotate (let’s say 360-1 = 359). It will return 250. Why I neet to to this?
You are correct about what Mathf.Min is doing. You want to do it for this reason:
We always want to rotate only by the smaller amount of “amountToRotateThisFrame” and “degreeLeftToRotate” for two reasons:
- We never want to rotate more than “amountToRotateThisFrame” because then we will rotate too much in one frame and the animation will not look smooth. It will “pop” too far ahead in the rotation.
- We never want to rotate more than “degreeLeftToRotate” because if we do, we will rotate PAST one full circle rotation. Let’s say we only have 5 degrees left to rotate to complete our full circle, but “amountToRotateThisFrame” wants us to rotate 10 degrees. We will then complete our circle plus do an extra 5 degree rotation, which means our camera will stop rotating but it will be slightly tilted. That won’t look good!
So we take the minimum of the two so we don’t break either of these rules.
1 Like
Clear as crystal! Thank you SO much, not only for the solution but to make me understad WHY I need to do certain things to accomplish what I need.
1 Like
No problem! Now go make a great game and share it with me!
1 Like