目录
轴映射与动作映射:SetupPlayerInputComponent
获取经过时间和剩余时间:GetTimerElapsed & GetTimerRemaining
虚幻引擎中的碰撞响应参考 | 虚幻引擎5.0文档 (unrealengine.com)
变量、定时器和事件 | 虚幻引擎5.0文档 (unrealengine.com)
虚幻引擎中的Gameplay定时器 | 虚幻引擎5.0文档 (unrealengine.com)

绑定名称及事件,Action还需绑定按键
- void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
- {
- Super::SetupPlayerInputComponent(PlayerInputComponent);
-
- PlayerInputComponent->BindAction("DropItem", EInputEvent::IE_Pressed, this, &AMyCharacter::DropItem);
- PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AMyCharacter::Jump);
- PlayerInputComponent->BindAxis("MoveForward", this, &AMyCharacter::MoveForward);
- PlayerInputComponent->BindAxis("MoveRight", this, &AMyCharacter::MoveRight);
- PlayerInputComponent->BindAxis("PitchCamera", this, &AMyCharacter::PitchCamera);
- PlayerInputComponent->BindAxis("YawCamera", this, &AMyCharacter::YawCamera);
- }
事件:
- void AMyCharacter::MoveForward(float AxisValue)
- {
- MovementInput.X = FMath::Clamp<float>(AxisValue, -1.f, 1.f);
- }
-
- void AMyCharacter::MoveRight(float AxisValue)
- {
- MovementInput.Y = FMath::Clamp<float>(AxisValue, -1.f, 1.f);
- }
-
- void AMyCharacter::PitchCamera(float AxisValue)
- {
- CameraInput.Y = AxisValue;
- }
-
- void AMyCharacter::YawCamera(float AxisValue)
- {
- CameraInput.X = AxisValue;
- }
设置按键,游戏暂停可以继续响应
InputComponent->BindAction("ESCEvent", IE_Pressed, this, &ASLAiPlayerController::ESCEvent).bExecuteWhenPaused=true;//游戏暂停可以执行
- //添加、绑定ActionKeyMapping轴映射 方法一
- FInputActionKeyMapping onFire("OnFire", EKeys::LeftMouseButton, 0, 0, 0, 0);
- UPlayerInput::AddEngineDefinedActionMapping(onFire);
- PlayerInputComponent->BindAction("OnFire", IE_Pressed, this, &AMyCharacter::OnFire);
-
- //添加、绑定ActionKeyMapping轴映射 方法二
- UPlayerInput::AddEngineDefinedActionMapping(FInputActionKeyMapping("Sprint",EKeys::LeftShift));
- PlayerInputComponent->BindAction("Sprint", IE_Pressed,this,&AMyCharacter::StartSprint);
- PlayerInputComponent->BindAction("Sprint", IE_Released,this,&AMyCharacter::StopSprint);
-
- //添加、绑定AxisMapping轴映射
- UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("Turn", EKeys::MouseX, 1.0f));
- PlayerInputComponent->BindAxis("Turn", this, &AMyCharacter::OnTurn);
-



- UPROPERTY(EditAnywhere)
- USceneComponent* Root;
-
- UPROPERTY(EditAnywhere)
- UStaticMeshComponent* Cube;
-
- UFUNCTION()
- virtual void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
-
- UFUNCTION()
- virtual void OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
-
- UFUNCTION()
- virtual void OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
- ACollisionActor::ACollisionActor()
- {
- // 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;
-
- Root = CreateDefaultSubobject
(TEXT("RootScene")); - SetRootComponent(Root);
-
- Cube = CreateDefaultSubobject
(TEXT("Cube")); - Cube->SetupAttachment(Root);
- static ConstructorHelpers::FObjectFinder
mesh(TEXT("StaticMesh'/Engine/BasicShapes/Cube.Cube'")) ; - if (mesh.Succeeded())
- {
- Cube->SetStaticMesh(mesh.Object);
- }
-
- // 设置是否开启物理模拟
- Cube->SetSimulatePhysics(false);
-
- // 开启 Generated Hit Event
- Cube->SetNotifyRigidBodyCollision(true);
-
- // 开启CCD Continuous collision detection (CCD) 连续式碰撞检测
- Cube->BodyInstance.SetUseCCD(true);
-
- // 开启Generate Overlap Events
- Cube->SetGenerateOverlapEvents(true);
-
- // 设置碰撞预设
- Cube->SetCollisionProfileName(TEXT("OverlapAll"));
- //Cube->SetCollisionResponseToAllChannels(ECR_Overlap);
-
- // 设置碰撞响应设置
- Cube->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
-
- // 绑定函数
- Cube->OnComponentBeginOverlap.AddDynamic(this, &ACollisionActor::OnOverlapBegin);
-
- // 绑定函数 使用委托
- FScriptDelegate OverlapEndDelegate;
- OverlapEndDelegate.BindUFunction(this, TEXT("OnOverlapEnd"));
- Cube->OnComponentBeginOverlap.Add(OverlapEndDelegate);
-
- // 绑定碰撞函数
- Cube->OnComponentHit.AddDynamic(this, &ACollisionActor::OnHit);
- }
- void ACollisionActor::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
- {
- UE_LOG(LogTemp, Warning, TEXT("Overlap Begin"));
- }
-
- void ACollisionActor::OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
- {
- UE_LOG(LogTemp, Warning, TEXT("Overlap End"));
- }
-
- void ACollisionActor::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
- {
- UE_LOG(LogTemp, Warning, TEXT("Hit"));
- Destroy();
- }
- template<class UserClass>
- void SetTimer
- (
- FTimerHandle & InOutHandle,
- UserClass * InObj,
- typename FTimerDelegate::TUObjectMethodDelegate_Const< UserClass >::FMethodPtr InTimerMethod,
- float InRate,
- bool InbLoop,
- float InFirstDelay
- )
- FTimerHandle SpawnerHandle;
-
- GetWorldTimerManager().SetTimer(SpawnerHandle, this, &AEnenmySpawner::SpawnEnemy, 5.0f, true);
-
- void AEnenmySpawner::SpawnEnemy()
- {
- GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Red, "Spawn");
- }
- GetWorldTimerManager().ClearTimer(SpawnerHandle);
-
- // 参数传入一个对象,本例以 this 举例
- GetWorldTimerManagerr().ClearAllTimersForObject(this);
- GetWorldTimerManager().PauseTimer(SpawnerHandle);
- GetWorldTimerManager().UnPauseTimer(SpawnerHandle);
- GetWorldTimerManager().IsTimerPaused(SpawnerHandle);
GetWorldTimerManager().IsTimerActive(SpawnerHandle);
句柄若无效则返回-1
GetWorldTimerManager().GetTimerRate(SpawnerHandle);
GetTimerElapsed & GetTimerRemaining
句柄若无效,返回-1
句柄若无效,返回-1
- GetWorldTimerManager().GetTimerElapsed(SpawnerHandle);
- GetWorldTimerManager().GetTimerRemaining(SpawnerHandle);