Unreal Engine 4 switch default camera implementation

Source: Internet
Author: User

By default, ue4 sets the camera in the defaultpawnclass object in agamemode as the default camera. Sometimes we want a global camera to observe the scene, which requires re-creating a camera and directly switching between the two cameras. The following describes how to implement it.

In the aplayercontroller class, there is a setviewtarget method. The parameter is an aactor pointer, which implements this function. Because the parameter must be aactor, that is, it must be the camera component contained in the aactor to be valid. For all, create an aactor object first.

Freecameraactor. h

Uclass ()
Class nantopdown_api afreecameraactor: Public aactor
Generated_uclass_body ()

Uproperty (visibleanywhere, blueprintreadonly, Category = camera)
Tsubobjectptr <class ucameracomponent> topdowncameracomponent;

Ufunction (blueprintcallable, Category = "view target ")
Void changeviewtarget ();

Virtual void beginplay () override;

Aactor * oldactor;

The changeviewtarget () method is provided to blueprint for switching between camera and freecamera of character.

Freecameraactor. cpp

Afreecameraactor: afreecameraactor (const class fpostconstructinitializeproperties & PCIP)
: Super (PCIP)
Topdowncameracomponent = PCIP. createdefadefasubobject <ucameracomponent> (this, text ("topdowncamera "));
This-> rootcomponent = topdowncameracomponent;
Oldactor = NULL;

Void afreecameraactor: changeviewtarget ()
Aactor * pactor = This-> getworld ()-> getfirstplayercontroller ()-> getviewtarget ();
If (pactor! = Oldactor)
This-> getworld ()-> getfirstplayercontroller ()-> setviewtarget (oldactor );
This-> getworld ()-> getfirstplayercontroller ()-> setviewtarget (this );

Void afreecameraactor: beginplay ()
Super: beginplay ();

Oldactor = This-> getworld ()-> getfirstplayercontroller ()-> getviewtarget ();

This-> getworld ()-> getfirstplayercontroller ()-> setviewtarget (this );

In the editor, create an actor inherited from afreecameraactor and drag and drop it to the scene. You can see an object that contains the camera. Then edit level blueprint and add the script code.

In the play scenario, press the 1 key to see the camera switch between character and the new camera.

Unreal Engine 4 switch default camera implementation

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.