Suppose I have a gameobject with a rigidbody that is moved by the player via WASD.
I write a class that inherits from mono, gets the player input and applies some movement in FixedUpdate. Great, the object now moves around as the player wishes.
Then I want to add a camera that follows this rigidbody. I do not want to parent the camera to the gameobject with the rigidbody. So I update the camera’s position in FixedUpdate to match the new rigidbody position.
Initially I thought I would separate out my camera class and apply it to the camera object, but this did not work, as I could not be sure that the movement of the rigidbody would happen before the camera class FixedUpdate…
I need to be sure that the camera follows the rigidbody as it moves and does not lag behind it by 1 physics cycle. So I have to apply my camera motion in the rigidbody movement class FixedUpdate after the application of the physics movement.
My camera class then gets much more involved as I want to allow various camera positioning functionality. My rigidbody movement class gets more involved as I add extra control options. I’m heading towards one big ugly pile of code that does several very different things and I feel sad.
I’m tied to both inheriting from mono and I’m tied to ensuring the rigidbody move is applied before the camera moves in that physics cycle.
What is the best approach to writing what appears to be 2 self contained classes, that aren’t quite self contained and can’t inherit from each other? Do I have to have a single controller class that then calls methods from both the movement and camera classes, or is there a better way?