UE4 C++碰撞和OverLap事件绑定
// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "GameFramework/Character.h" #include "MyCharacter.generated.h" UCLASS() class PACMAN_API AMyCharacter : public ACharacter { GENERATED_BODY() public: // Sets default values for this characters properties AMyCharacter(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; // Called to bind functionality to input virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; UFUNCTION() void OnCollision( UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit); UFUNCTION() void OnOverLap( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult); };
// Fill out your copyright notice in the Description page of Project Settings. #include "MyCharacter.h" #include "Components/CapsuleComponent.h" #include "Collecable.h" // Sets default values AMyCharacter::AMyCharacter() { // Set this character to call Tick() every frame. You can turn this off to improve performance if you dont need it. PrimaryActorTick.bCanEverTick = true; } // Called when the game starts or when spawned void AMyCharacter::BeginPlay() { Super::BeginPlay(); //怎么查看回调函数的参数,对OnComponentHit右键转到定义,去掉第一个参数 GetCapsuleComponent()->OnComponentHit.AddDynamic(this,&AMyCharacter::OnCollision); GetCapsuleComponent()->OnComponentBeginOverlap.AddDynamic(this, &AMyCharacter::OnOverLap); } // Called every frame void AMyCharacter::Tick(float DeltaTime) { Super::Tick(DeltaTime); } // Called to bind functionality to input void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent); } void AMyCharacter::OnCollision(UPrimitiveComponent * HitComponent, AActor * OtherActor, UPrimitiveComponent * OtherComp, FVector NormalImpulse, const FHitResult & Hit) { UE_LOG(LogTemp, Warning, TEXT("Collision")); } void AMyCharacter::OnOverLap(UPrimitiveComponent * OverlappedComponent, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult) { UE_LOG(LogTemp, Warning, TEXT("OverLap")); //ACollecable 一个自定义类 if (OtherActor->IsA(ACollecable::StaticClass())) { OtherActor->Destroy(); } }
下一篇:
Ajax向后台传多个对象和数组