Component Based Design and Camera Spring

Hi,

After a little exploration I’ve become very excited by the idea of Component Based Design in conjunction with Unity, but having had almost no experience with it, I’m not sure how best to start.

I therefore have two questions to ask the community, one broad and one specific:

  1. The broad question: are there any guidelines or standard programming practices on how best to develop a good component based architecture with unity?

  2. The specific question: I have been writing my own behaviour components for a camera system (3rd person chase cam, rails cam, fixed tracking, etc) and am now looking at writing other modules such as ‘shakey camera’ and ‘camera spring’ which I could use at the same time as one of the behaviour scripts - e.g. a chase cam with shakiness, or a rails cam with springs.

However, as both scripts would have code to change the position and lookat values of the camera, how can I control their combined behaviour? I thought that maybe I could control the order in which the script Update() methods are called, but it looks like that would be incredibly unreliable.

Am I doomed to have to simply include ‘shake’ and ‘spring’ code into each of the camera behaviour scripts and use a bool to turn them on and off? that kind of code duplication makes me weep.

I feel sure that this is the exact kind of thing that that Component Based Design is made for but am not sure how to use it properly. Can anyone help?

Thanks a lot.

Perhaps instead of movement adjustments every frame, you can have a “goal” for the camera which is lerped to.

If you decide to use multiple scripts, each script’s goal can be taken and averaged in a way that you feel suits the game, to create the ‘actual’ goal.

Alternatively, (especially for shakyness), you can nest the camera in an empty object with the shake script applied to that object. This way the camera’s normal scripts can run normally, and when you want it to shake, the parent’s shaking won’t interfere with the camera’s script (as long as you use localPosition details instead of global position).

In theory you could put the regular camera script onto the camera, put the spring and shake onto two empty game objects (named “Springer” and “Shaker” for example),

Then put the Camera in the springer, and the springer in the shaker.

Shaker
->Springer
—>Camera

That way the camera is affected by all 3 scripts as necessary, without interrupting its own script.