我想在按下鼠标左键时打开门,但是什么也没发生

c++ - OnMousePress脚本在UE4中不起作用-LMLPHP

OpenDoor.h

UCLASS()
class HOME_API ADoorOpen : public APawn
{
  GENERATED_BODY()

public:
  ADoorOpen();

protected:
  virtual void BeginPlay() override;

public:
  virtual void Tick(float DeltaTime) override;

  virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

  UPROPERTY()
  USceneComponent* Root;

  UPROPERTY(EditAnywhere)
  UStaticMeshComponent* Door;

  UPROPERTY(EditAnywhere, Category = "DoorOpen")
  float rotateValue;

  UPROPERTY(BlueprintReadOnly, Category = "DoorOpen")
  FRotator doorRotation;

  UPROPERTY(BlueprintReadOnly, Category = "DoorOpen")
  bool open;

  UFUNCTION(BlueprintCallable, Category = "DoorOpen")
  void OpenCloseDoor();
};

OpenDoor.cpp
#include "DoorOpen.h"
#include "DrawDebugHelpers.h"
#include "Engine.h"

ADoorOpen::ADoorOpen()
{
  PrimaryActorTick.bCanEverTick = true;

  rotateValue = 0.0f;
  open = false;

  Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
  RootComponent = Root;

  Door = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Door"));
  Door->SetRelativeLocation(FVector(0.f, 50.f, -50.f));
  Door->SetupAttachment(RootComponent);
}

void ADoorOpen::OpenCloseDoor()
{
  // don't see this while playing
  GEngine->AddOnScreenDebugMessage(-1, 0.f, FColor::Red, "OpenCloseDoor");
  //...
}

void ADoorOpen::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
  Super::SetupPlayerInputComponent(PlayerInputComponent);

  // Can't call this
  PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ADoorOpen::OpenCloseDoor);
}

最佳答案

增加AddOnScreenDebugMessage的显示时间

GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Red, "OpenCloseDoor");

否则,文本将仅显示一个框架。

10-06 04:21