Another great lecture! Wanted to call out that the addition of the refueling sphere reveals an interesting bug (on Unity 2020.3.30f1 at least).
In an earlier lecture we’d added Freeze Position and Freeze Rotation constraints on the rocket to keep it from moving into or out of the screen. So I was surprised to see Rick’s rocket collide with the sphere, then miss the landing pad entirely by moving past it in the Z direction. The rocket’s motion should have been constrained to the XY plane because of the Rigidbody constraints we’d set.
Apparently, setting rb.freezeRotation = false;
in our ApplyRotation
method resets our rotation constraint and allows the rocket to rotate around the X-axis, then boost forward into the +Z direction. FWIW I don’t get quite the same behavior as Rick; my rocket’s rotation constraint is lost, but its position constraint is preserved so it never actually moves such that it can no longer land on the landing pad.
In any case, one fix for this is to preserve, then restore, the original Rigidbody constraints when we are using freezeRotation
:
void ApplyRotation(float rotationThisFrame)
{
// Preserve constraints because un-freezing rotation will reset them
RigidbodyConstraints originalConstraints = rb.constraints
rb.freezeRotation = true; // freezing rotation so we can manually rotate
transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);
rb.freezeRotation = false; // unfreezing rotation so the physics system can take over
// Restore original constraints
rb.constraints = originalConstraints;
}