I’m using Instantiate in my block breaker game to drop leaves from a tree when the ball hits it. I have a Serialized Field on my Tree script and I’ve placed my Leaf prefab in there in the editor. When I hit the tree with the ball it spawns leaves, which then slowly drop to the ground. Jackpot.
However, within my Leaf prefab, I have a Leaf script that is supposed to be rotating the leaf on the Z-axis to simulate the leaf gently floating down (it’d probably be better to use the actual particle system to do that, but I’m giving it a shot based just on what I’ve learned from the tutorials so far). When I look at my prefab the Z-axis rotation of the leaf is at 0.
In my Leaf.cs I’m declaring an enum to keep track of if the leaf is currently rotating left or right, I set the speed, and declare some extra variables:
enum FloatDirection {
Left = 1,
Right = 2
}
[SerializeField] float RotateSpeed = 0.4f;
float LowRotationRange;
float HighRotationRange;
float CurrentRotation;
FloatDirection CurrentDirection;
Then in my Start method, I’m randomly generating how far to the left and right the leaves can rotate (so they don’t all look identical):
HighRotationRange = UnityEngine.Random.Range(100f, 250f);
LowRotationRange = HighRotationRange * -1;
And randomly generate somewhere in that range for the rotation to start (so they don’t all start in exactly the same spot):
CurrentRotation = UnityEngine.Random.Range(LowRotationRange, HighRotationRange);
I randomly decide if it’s starting going left or right:
if (UnityEngine.Random.Range(1, 2) == 1) {
CurrentDirection = FloatDirection.Left;
} else {
CurrentDirection = FloatDirection.Right;
}
And then I set the rotation to my newly generated value:
transform.rotation = new Quaternion(0, 0, CurrentRotation, 0);
So my full Start method looks like this:
void Start() {
HighRotationRange = UnityEngine.Random.Range(100f, 250f);
LowRotationRange = HighRotationRange * -1;
CurrentRotation = UnityEngine.Random.Range(LowRotationRange, HighRotationRange);
if (UnityEngine.Random.Range(1, 2) == 1) {
CurrentDirection = FloatDirection.Left;
} else {
CurrentDirection = FloatDirection.Right;
}
transform.rotation = new Quaternion(0, 0, CurrentRotation, 0);
}
For my Update method, I check if the direction of the leaf needs to change, I figure out what the new rotation should be, and I set that new rotation:
void Update() {
CheckIfDirectionNeedsToChange();
AdjustRotation();
ApplyRotation();
}
Within CheckIfDirectionNeedsToChange I see if it’s floating to the left and has passed the minimum rotation value or if it’s floating to the right and has passed the maximum rotation value; if either of those are true I change the direction:
private void CheckIfDirectionNeedsToChange() {
if (CurrentDirection == FloatDirection.Left && CurrentRotation < LowRotationRange) {
CurrentDirection = FloatDirection.Right;
} else if (CurrentDirection == FloatDirection.Right && CurrentRotation > HighRotationRange) {
CurrentDirection = FloatDirection.Left;
}
}
Within AdjustRotation I either add (if it’s floating right) or subtract (if it’s floating left) the RotateSpeed value to get an updated CurrentRotation value:
private void AdjustRotation() {
if (CurrentDirection == FloatDirection.Left) {
CurrentRotation -= RotateSpeed;
} else {
CurrentRotation += RotateSpeed;
}
}
Lastly, in ApplyRotation I update the rotation of the transform and print out the value to the Debug log so I can see if it’s working:
private void ApplyRotation() {
Debug.Log($"Changing Z rotation to {CurrentRotation}");
transform.rotation = new Quaternion(0, 0, CurrentRotation, 0);
}
The issue I’m having is that my Debug log gives me updated values every frame that go from the minimum value to the maximum value and back again, but it doesn’t actually rotate my GameObject, which seems like it should be fine (and in fact works fine in other parts of my game when I want to flip a sprite from facing left to facing right, though there I’m rotating on the Y-axis). Even odder, if I pause the game after the leaf is Instantiated and click on it in the editor its Z rotation is always set to -180.
Any idea what I might have done wrong here? The logic seems right in my brain, and the Debug log seems to confirm that, but yet no rotation is actually happening.
Thanks in advance for any fixes/ideas.