u4初步使用整理(一)

    1.关键方法

    1. GetActorForwardVector(),获取Actor前进方向,以ArrorwComponent方向一致。
    2. GetActorRightVector()
    3. AddMovementInput()
    4. AddControllerYawInput()
    5. AddControllerPitchInput()
    6. InputComponent->BindAxis("Pitch", this, &AAvatar::Pitch); 输入绑定需要先在config里做按键配置。

    2.注意问题

    1. 动态添加USphereComponent,需要重写构造函数,并初始化,否则崩溃。

      public:
      ANPC(const FObjectInitializer& ObjectInitializer);
      public:
      UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision)
      TSubobjectPtr<class USphereComponent> ProxSphere;
      
      ANPC::ANPC(const FObjectInitializer& ObjectInitializer)
      {
          ProxSphere = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this,   TEXT("ProxSphere"));
      }
      
    2. UPROPERTY EditAnywhere 能在bp中修改的变量需要为public。
    3. A Private function cannot be a BlueprintNativeEvent
    4. 基于c++类建bp,一定要在bp下compile,这是个坑,c++里动态加入的component需要compile下才能更新到bp。
    5. sphere碰撞触发处理: UFUNCTION(BlueprintNativeEvent, Category = "Collision") void Prox(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

      ANPC::ANPC(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
      {
          TTTProxSphere = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("XXXXXSphere"));
          TTTProxSphere->AttachTo(RootComponent);
          TTTProxSphere->SetSphereRadius(50.f);
          TTTProxSphere->OnComponentBeginOverlap.AddDynamic(this, &ANPC::Prox);
          NpcMessage = "Default  Message.";
      }
      
      void ANPC::Prox_Implementation(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
      {
          if (Cast<AAvatar>(OtherActor) == nullptr)
          {
              return;
          }
          APlayerController *PController = GetWorld()->GetFirstPlayerController();
          if (PController)
          {
              AMyHUD *hud = Cast<AMyHUD>(PController->GetHUD());
              hud->addMessage(Message(NpcMessage, 5.f, FColor::White));
          }
      }
      

你可能感兴趣的:(u4初步使用整理(一))