Unreal Engine 4 C + + creates a zoom camera (source code) for the role

Source: Internet
Author: User

It is a more common requirement to watch the role of the camera in the game, and zooms is a more familiar one, which is simpler to implement in the UE4. This article takes the Topdown template as an example to illustrate how to implement a zoom camera step-by-step.

After creating a C + + project of the Topdown template, we get 3 important classes. The Axxxcharacter class, which implements the control and behavior of the role. The Axxxgamemode class, the default gamemode for the project, defines the default role controller and default pawn. Axxxplayercontroller, the Role controller class, accepts input control, and directs the role to act. We mainly modify the Axxxcharacter class.

The principle of zoom implementation, UE4 specifically for the camera collision implementation of a uspringarmcomponent class, it can automatically deal with camera-related collisions, indentation and other functions. To achieve zoom, you only need to properly adjust the value of the targetarmlength variable of this class. Very simple.

First, we define the zoom configuration parameters. Add the following structure to the XXXCharacter.h file:

Ustruct () struct fzoomdata{generated_ustruct_body () uproperty (editdefaultsonly, Category = Config) float Mincameralen; Uproperty (editdefaultsonly, Category = Config) float Maxcameralen; Uproperty (editdefaultsonly, Category = Config) float Zoomsteplen;};
Mincameralen, defines the camera's nearest distance, Maxcameralen, defines the camera's farthest distance, Zoomsteplen, defines the distance of each small step, zoom. Note that the usruct () prefix, which can be configured in the editor itself, is behind the structure.

The second step is to add configuration parameters for the Axxxcharacter class.

Uproperty (editdefaultsonly, Category = Config) Fzoomdata zoomconfig;

The third step is to add the member ZoomIn and Zoomout methods for Axxxcharacter. These two methods, specifically to achieve the zoom function.

Ufunction (blueprintcallable, category= Camera) void ZoomIn (); Ufunction (blueprintcallable, Category = Camera) void Zoomout ();

Fourth step, add the overloaded method setupplayerinputcomponent for Axxxcharacter. This method initializes the input binding.

protected:virtual void Setupplayerinputcomponent (class uinputcomponent* inputcomponent) override;


The following are the specific implementations of each method:

void Asqxgamecharacter::setupplayerinputcomponent (class uinputcomponent* inputcomponent) {check (inputcomponent); Nputcomponent->bindaction ("ZoomIn", ie_pressed, this, &asqxgamecharacter::zoomin);inputcomponent-> Bindaction ("Zoomout", ie_pressed, this, &asqxgamecharacter::zoomout);
The Setupplayerinputcomponent method binds two actions named ZoomIn and Zoomout, and these two actions are set in the editor. Under the menu Edit->project settings->input item.

I have two actions bound to mouse Wheel up and mouse Wheel down two keys respectively.


void Asqxgamecharacter::zoomin () {if (cameraboom->targetarmlength >= zoomconfig.mincameralen) {CameraBoom-> Targetarmlength-= Zoomconfig.zoomsteplen;} Else{cameraboom->targetarmlength = Zoomconfig.mincameralen;}} void Asqxgamecharacter::zoomout () {if (cameraboom->targetarmlength <= zoomconfig.maxcameralen) {CameraBoom- >targetarmlength + = Zoomconfig.zoomsteplen;} Else{cameraboom->targetarmlength = Zoomconfig.maxcameralen;}}
The implementation of the ZoomIn and Zoomout methods is simple and does not explain.

Compile the code and set the value of the Zoomconfig in the character blueprint, and execute the game to see the results.


Project Source code: Http://git.oschina.net/cloudsource/UE4-Code

Unreal Engine 4 C + + creates a zoom camera (source code) for the role

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.