Actor操作
移动
FVector CurrentLocation = GetActorLocation();
// 在X轴上增加移动
CurrentLocation.X += MoveSpeed * DeltaTime;
// 应用新的位置
SetActorLocation(CurrentLocation);
AddActorLocalOffset(FVector(MoveSpeed * DeltaTime, 0.0f, 0.0f));
// 获取当前相对位置
FVector CurrentRelativeLocation = GetActorLocation();
// 在X轴上增加相对移动
CurrentRelativeLocation.X += MoveSpeed * DeltaTime;
// 应用新的相对位置
SetActorRelativeLocation(CurrentRelativeLocation);
// 获取当前位置
FVector CurrentLocation = GetActorLocation();
// 在X轴上增加移动
CurrentLocation.X += MoveSpeed * DeltaTime;
// 使用插值实现平滑移动
FVector NewLocation = FMath::VInterpTo(GetActorLocation(), CurrentLocation, DeltaTime, 5.0f);
// 应用新的位置
SetActorLocation(NewLocation);
// 获取玩家控制器
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();if (PlayerController)
{// 获取玩家输入float ForwardInput = 0.0f;PlayerController->InputComponent->GetAxisValue("MoveForward", ForwardInput);float RightInput = 0.0f;PlayerController->InputComponent->GetAxisValue("MoveRight", RightInput);// 构建移动方向FVector MoveDirection = FVector(ForwardInput, RightInput, 0.0f).GetSafeNormal();// 在本地坐标系上根据输入控制移动AddActorLocalOffset(MoveDirection * MoveSpeed * DeltaTime);
}
旋转
FRotator CurrentRotation = GetActorRotation();
CurrentRotation.Yaw += RotationSpeed * DeltaTime;
SetActorRotation(CurrentRotation);
//
SetActorRelativeRotation(CurrentRotation);
//
AddActorLocalRotation(FRotator(0.0f, RotationSpeed * DeltaTime, 0.0f));
// 获取当前四元数旋转
FQuat CurrentRotationQuat = GetActorQuat();
// 创建一个Quat,表示绕Z轴的旋转
FQuat RotationQuat = FQuat(FRotator(0.0f, RotationSpeed * DeltaTime, 0.0f));
// 将当前旋转与新旋转相乘
FQuat NewRotationQuat = CurrentRotationQuat * RotationQuat;
// 应用新的旋转
SetActorRotation(NewRotationQuat.Rotator());
// 获取当前旋转
FRotator CurrentRotation = GetActorRotation();
// 在Yaw轴上增加旋转
CurrentRotation.Yaw += RotationSpeed * DeltaTime;
// 使用插值实现平滑旋转
FRotator NewRotation = FMath::RInterpTo(GetActorRotation(), CurrentRotation, DeltaTime, 5.0f);
// 应用新的旋转
SetActorRotation(NewRotation);
// 获取玩家控制器
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();if (PlayerController)
{// 获取玩家输入float YawInput = 0.0f;PlayerController->InputComponent->GetAxisValue("YawAxis", YawInput);// 在Yaw轴上根据输入控制旋转FRotator NewRotation = GetActorRotation() + FRotator(0.0f, YawInput * RotationSpeed * DeltaTime, 0.0f);// 应用新的旋转SetActorRotation(NewRotation);
}
组件操作
组件创建
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
//
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
组件查找
//名称查找
UBoxComponent* BoxComponent = Cast<UBoxComponent>(MyActor->GetComponentByName(TEXT("BoxComponent")));
//类型查找
UBoxComponent* BoxComponent = Cast<UBoxComponent>(MyActor->GetComponentByClass(UBoxComponent::StaticClass()));
//标签查找
TArray<UActorComponent*> Components;
MyActor->GetComponentsByTag(UIText::StaticClass(), TEXT("MyTag"), Components);
时间获取:
GetWorld()->GetTimeSeconds();
GetWorld()->TimeSince(18.6825);
定时器操作
GetWorldTimerManager().SetTimer(TimerHandle_Interaction,this,&ACSTutorialCharacter::Interact,TargetInteractable->InteractableData.InteractionDuration,false);
GetWorldTimerManager().ClearTimer(TimerHandle_Interaction);
GetWorldTimerManager().IsTimerActive(TimerHandle_Interaction)
事件绑定
//Jumping
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
//Moving
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ACSTutorialCharacter::Move);
//Looking
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ACSTutorialCharacter::Look);
//Interacting
EnhancedInputComponent->BindAction(InteractAction, ETriggerEvent::Started, this, &ACSTutorialCharacter::BeginInteract);
EnhancedInputComponent->BindAction(InteractAction, ETriggerEvent::Completed, this, &ACSTutorialCharacter::EndInteract);
功能调试
调试绘制
DrawDebugLine(GetWorld(), TraceStart, TraceEnd, FColor::Red, false, 1, 0, 1);