

In XCode, locate the Project Navigator, which by default is on the left side of the window, then use it to locate FloatingActor.h. Now that we have created our C++ Class, we're going to switch over to XCode and edit our code.

In this case, it will make our cube simultaneously rotate while also floating up and down.įor more information about Ticking actors, see The Tick function is where we add code that we want to execute in real-time. SetActorLocationAndRotation(NewLocation, NewRotation) NewLocation.Z += DeltaHeight * 20.0f //Scale our height by a factor of 20įloat DeltaRotation = DeltaTime * 20.0f //Rotate by 20 degrees per second For more information about attaching components in code, refer to our guide for Creating and Attaching Components.Īdd the following code inside of AFloatingActor::Tick(float DeltaTime), just before the closing bracket:įVector NewLocation = GetActorLocation() įRotator NewRotation = GetActorRotation() įloat RunningTime = GetGameTimeSinceCreation() įloat DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime)) The code we have added will fill our VisualMesh reference in with a new StaticMeshComponent,Īttach it to our Actor, and set it to the cube mesh from the Starter Content assets. This function is the constructor, and it tells the class how to initialize itself when it is first created. VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f)) Static ConstructorHelpers::FObjectFinder CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube")) VisualMesh->SetupAttachment(RootComponent) VisualMesh = CreateDefaultSubobject(TEXT("Mesh")) Now open FloatingActor.cpp and add the following code inside of AFloatingActor::AFloatingActor(), just before the closing bracket: Note that it uses a UProperty macro, which makes it visible inside the Unreal Editor.įor more information on UProperty and its specifiers, see the page on Properties. Here we're declaring a StaticMeshComponent, which will act as our visual representation for our object. You can think of it as being sort of like a table of contents for a C++ class.īefore we can start building any new functionality, we must declare any new Variables or Functions that we're using in this file.Īdd the following code underneath the declaration for AFloatingActor(): In our project, it will be located under Games > QuickStart > Source > QuickStart.ĭouble-click FloatingActor.h to open it and bring it into focus in the text editor. In Visual Studio, locate the Solution Explorer, which by default is on the left side of the window, then use it to locate FloatingActor.h. Now that we have created our C++ Class, we're going to switch over to Visual Studio and edit our code.
