UC++常用语句示例

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);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值