Let's say I have a RotatableObject
class, which inherits from MonoBehaviour
. Here is its Start()
method:
private void Start() {
this.startingLocalRotation = this.transform.localRotation;
this.finalLocalRotation = Quaternion.Euler(this.startingLocalRotation.eulerAngles.x, -150f, this.startingLocalRotation.eulerAngles.z);
// Initialize other private fields
}
It has a method called Rotate()
, defined as follows:
private void Rotate() {
this.transform.localRotation = Quaternion.Lerp(this.startingLocalRotation, this.finalLocalRotation, 0.2f);
}
Given the following Update()
method, the object rotates as expected:
private void Update() {
this.Rotate();
}
However, if I make the following changes to Update()
, then the object does not actually rotate, even though the Rotate()
method is hit:
private void Update() {
if (this.SomeCondition) {
this.Rotate();
}
}
this.SomeCondition
always has the value true
over multiple frames.
What is my mistake, and how can I fix it?
EDIT:
In response to this comment:
When you say "the object rotates as expected" do you mean that the object rotates from starting angles until final angles and then stops rotating (without the if) ?
Yes.
could you provide some more details about what
SomeCondition
is?
this.SomeCondition
is determined by the state of another object, say, Puppy
. E.g. if puppyInstance.Mood == Mood.Happy
, then this.SomeCondition
is true
.
Did you try replacing the condition with the word
true
No, I have not tried that. However, when stepping through the call stack in debug mode, I can see that this.SomeCondition
is true
and Rotate()
is called -- I managed to step into the Rotate()
method itself.
EDIT:
I tried the first approach described in this answer. However, it did not produce the effects that I was aiming for.
I set a breakpoint here:
transform.localRotation = Quaternion.Lerp(
startingLocalRotation,
finalLocalRotation,
_rotationProgress
);
Every single time the breakpoint was hit in the Update()
call, I could see that the value of _rotationProgress
was steadily increasing. However, the value of transform.localRotation
never actually changed -- it was always (-0.7, 0.7, 0.0, 0.0).
If I updated transform.localRotation
outside of an if
clause, however, that game object rotates as expected -- transform.localRotation
gets updated as _rotationProgress
gets updated.
EDIT:
Never mind, I am an idiot. The problem lies elsewhere in the code. The suggested approach works and I have accepted it as the answer.
SomeCondition
is? Did you try replacing the condition with the wordtrue
, did you put aDebug.Log
before theRotate
to make sure the if is running properly? \$\endgroup\$if
, and as I expected, your object does not rotate from start to final angles, instead it keeps the rotation based on theLerp
with t=0.2f
. I assume this means you tried to simplify your code and failed, so you might want to edit that. \$\endgroup\$Rotate
you set thelocalRotation
to be aLerp
between the beginning and end of your angles, but for the last argument you give a constant of0.2f
which means it should return the exact same rotation everytime it runs, which becomes the rotation of your object. If your object is rotating, it means that you are running different code, or that object's parent is rotating, and as a result this one is rotating too. \$\endgroup\$