UE4笔记C++篇---Actor物体上下移动


UE4笔记C++篇---Actor物体上下移动_第1张图片

UE4笔记C++篇---Actor球物体上下移动

C++文件:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Actor.h"
#include "Ball.generated.h"

UCLASS()
class TASK_API ABall : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ABall();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	float RunningTime;

	void Print_T(FString InStr);
	
};

// Fill out your copyright notice in the Description page of Project Settings.

#include "Task.h"
#include "Ball.h"
#include "UnrealMathUtility.h"


// Sets default values
ABall::ABall()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void ABall::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void ABall::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

	// 获取位置
	FVector NewLoacation = GetActorLocation();
	// 防止浮点无限大导致不精确,每2*PI一个循环
	if (RunningTime >= 2 * PI)
	{
		RunningTime = 0;
	}
	// 获取高度变化曲线
	float DeltaHeight = FMath::Sin(RunningTime);
	// DeltaTime是上一帧和下一帧之间使用的时间
	RunningTime += DeltaTime;	
	
	// 20是沿着Z轴的伸缩变化值
	NewLoacation.Z += DeltaHeight /** 20.f*/;
	SetActorLocation(NewLoacation);
}

void ABall::Print_T(FString InStr)
{
	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, InStr);
	}
}



你可能感兴趣的:(UE4学习笔记,UE4笔记C++篇)