Customizing Existing Components: ControlAim as An Example
Last updated
Last updated
Sometimes you want to slightly modify existing components rather than implementing a new component from scratch. ComponentCameraSystem offers an extensible solution for you to override existing components, but in a limited way.
Concretely, you can override an existing component, say ScreenFollow, to incrementally add functionality to it. But you cannot manipulate the implementation details of the component. You can only complement additional logic after the component has been executed. If you are familiar with programming, this is equivalent to first calling the parent method and then doing anything else.
Let us take ControlAim as an example. This component receives player input to control camera orientation. I want to implement an "auto-rotating" function, where camera will automatically rotate around the follow target if no input is received for a specific period of time.
Right click on Content Browser -> select "Blueprint Class" -> Search for "ControlAim" -> click "Select".
Rename it as whatever you want. Here I name it MyControlAim. Open the blueprint class and you can see the "Functions" section on the left side of the blueprint window. There are several implementable functions in this section. For this component, you should implement the OnBecomeViewTarget and UpdateComponent functions, while other functions, if meet your needs, can also be implemented.
Define some variables. FollowTarget
is used to detect whether the follow target is moving. WaitingTime
is the duration to wait for enabling auto-rotation. RotateSpeed
is the self-rotation speed. CachedFollowTargetLocation
is follow target's location at previous frame. ElapsedWaitTime
is the elapsed time since no input is given.
In the OnBecomeViewTarget function, you should set FollowTarget
.
In the UpdateComponent function, first call the parent method, then call your custom DoSomething function.
The DoSomething function is the place where you implement self-rotation. Below is a reference implementation but feel free to make your own. The workflow is simple: first check whether the follow target is moving or input rotation is received (via the GetMouseDeltaX and GetMouseDeltaY nodes). If true, reset ElapsedWaitTime
, otherwise add increment to it. Next check if ElapsedWaitTime
has exceeded WaitingTime
, and if true, add rotation to camera. Last, update CachedFollowTargetLocation
. It is particularly noteworthy when using FollowTarget
I did not check its validity. But technically you should do this whenever you use it.
In the camera actor (of class ECameraBase
or sub-class of ECameraBase
), specify MyControlAim to the "Aim Component" property and set "Follow Component" with ScreenFollow. Go to the blueprint where you want to call this camera. Invoke the CallCamera node and specify "Camera Class" and "Follow Target". You can also specify blending parameters if you like.
Run the game, wait for WaitTime
seconds, and you will see camera is smoothly rotating around the follow target. When character moves or camera input is given, self-rotation will stop and you have to wait another WaitTime
to enable it.