OnMousePress脚本在UE4中不起作用

时间:2018-05-21 10:05:47

标签: c++ unreal-engine4

我想按下鼠标左键打开门,但没有任何反应

enter image description here

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

1 个答案:

答案 0 :(得分:1)

增加AddOnScreenDebugMessage的显示时间

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

否则,文本将只显示一帧。