[英]how to make a collision of an actor on a character in C++ UE4?
我希望在 c++ 中制作包含虚幻引擎功能的项目,例如:
当玩家踩到它时,他会赢得蘑菇效果:
所以我创建了我的 Actor 项目,其中包含 beginoverlap 和 power function:Item.h
#pragma once
#include "CoreMinimal.h"
#include "Components/CapsuleComponent.h"
#include "Components/SphereComponent.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/PlayerController.h"
#include "Character/Projet2Character.h"
#include "Item.generated.h"
UCLASS()
class PROJET2_API AItem : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AItem();
protected:
UPROPERTY(EditAnywhere)
USphereComponent* Collider;
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
UFUNCTION()
void OnBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION()
void OnEndOverlap(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
UFUNCTION()
void Power();
AProjet2Character* player;
// Called every frame
virtual void Tick(float DeltaTime) override;
};
项目.cpp
#include "Actor/Item.h"
// Sets default values
AItem::AItem()
{
// 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;
Mesh = CreateDefaultSubobject<UStaticMeshComponent>(FName("Mesh"));
Mesh->SetupAttachment(RootComponent);
RootComponent = Mesh;
Collider = CreateDefaultSubobject<USphereComponent>(FName("Collider"));
Collider->SetupAttachment(Mesh);
}
// Called when the game starts or when spawned
void AItem::BeginPlay()
{
Super::BeginPlay();
Collider->OnComponentBeginOverlap.AddDynamic(this, &AItem::OnBeginOverlap);
Collider->OnComponentEndOverlap.AddDynamic(this, &AItem::OnEndOverlap);
}
void AItem::OnBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if(OtherActor->IsA(AProjet2Character::StaticClass()))
{
Power();
}
}
void AItem::OnEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex)
{
if(OtherActor->IsA(AProjet2Character::StaticClass()))
{
Power();
}
}
void AItem::Power()
{
player->GetMesh()->SetRelativeScale3D(FVector(1.5f,1.5f,1.5f));
}
// Called every frame
void AItem::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
当我在与演员接触的那一刻启动游戏时,虚幻关闭并且效果仍然不适用于角色。 我想知道怎么做? :)
感谢您的理解
我不太明白。 您希望玩家踩在 object 上并增大尺寸,然后当他离开 object 时,将其恢复到原来的尺寸(1)还是保持尺寸(2)?
//.h
UFUNCTION()
void Power();
UFUNCTION()
void ResetPower();
bool bIsPower = false;
//.cpp
void AItem::ResetPower()
{
player->GetMesh()->SetRelativeScale3D(FVector(1.f,1.f,1.f));
}
1:
//.cpp
void AItem::OnBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if(OtherActor->IsA(AProjet2Character::StaticClass()) && !bIsPower)
{
player = Cast<AProjet2Character>(OtherActor);
bIsPower = true;
Power();
}
}
void AItem::OnEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex)
{
if(OtherActor->IsA(AProjet2Character::StaticClass()) && bIsPower)
{
player = Cast<AProjet2Character>(OtherActor);
bIsPower = false;
ResetPower();
}
}
2:
void AItem::OnBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if(OtherActor->IsA(AProjet2Character::StaticClass()) && !bIsPower)
{
player = Cast<AProjet2Character>(OtherActor);
bIsPower = true;
Power();
}
}
void AItem::OnEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex)
{
//Clear
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.