【发布时间】:2019-09-17 08:11:56
【问题描述】:
我遇到了碰撞问题。我想做简单的弹丸。我制作了一个具有 Root、Sphere 和网格组件的 Actor。当这个演员击中目标对象时,什么也没有发生。 我尝试了很多方法,也有
void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
函数,但没有任何结果..
这是我的自定义射弹的构造函数:
AProjectile_2::AProjectile_2()
{
// 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;
Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
RootComponent = Root;
mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("staticMesh"));
static ConstructorHelpers::FObjectFinder<UStaticMesh>SphereMeshAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
if (SphereMeshAsset.Succeeded())
{
mesh->SetStaticMesh(SphereMeshAsset.Object);
mesh->SetWorldScale3D(FVector(0.2f));
}
mesh->SetEnableGravity(true);
mesh->SetSimulatePhysics(true);
mesh->SetupAttachment(Root);
Sphere = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
Sphere->InitSphereRadius(5.f);
Sphere->SetupAttachment(Root);
Sphere->SetCollisionProfileName(TEXT("Target"));
Sphere->OnComponentHit.AddDynamic(this, &AProjectile_2::OnHit);
Sphere->SetupAttachment(RootComponent);
InitialLifeSpan = 5.f;
}
这是一个检测函数
void AProjectile_2::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL))
{
if (OtherActor && (OtherActor != this) && OtherComp && OtherActor->GetClass()->IsChildOf(ADestroypack::StaticClass()))
{
destroypack = (ADestroypack*)OtherActor;
destroypack->decreasehp(50);
destroypack->check_hp();
destroypack->showhp();
}
}
}
我的网格在这里移动
void AProjectile_2::Initvel(FVector Velocity)
{
mesh->AddForce(Velocity * 300);
}
这是我用人民币按钮拍摄的角色功能
void AHero::Attack_right()
{
FVector Location = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation();
FRotator Rotation = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorRotation();
GetActorEyesViewPoint(Location, Rotation);
projectiles = (AProjectile_2*) GetWorld()->SpawnActor(AProjectile_2::StaticClass(), &Location, &Rotation);
FVector LaunchDir;
LaunchDir = GetActorForwardVector() * 5000.f;
projectiles->Initvel(LaunchDir);
}
如果有人能说出我做错了什么,或者我的项目中缺少什么,我将不胜感激。
好的!我已经弄清楚了! :D 我在网格和球体中添加重力和物理时犯了一个错误。我已经从网格中删除了它,它现在可以工作了:)
AProjectile_2::AProjectile_2()
{
// 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;
//Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
//RootComponent = Root;
Sphere = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
Sphere->SetSimulatePhysics(true);
Sphere->SetEnableGravity(true);
Sphere->SetNotifyRigidBodyCollision(true);
Sphere->InitSphereRadius(40.f);
Sphere->BodyInstance.SetCollisionProfileName("BlockAllDynamic");
Sphere->SetCollisionProfileName(TEXT("Target"));
Sphere->OnComponentHit.AddDynamic(this, &AProjectile_2::OnHit);
RootComponent = Sphere;
mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("staticMesh"));
static ConstructorHelpers::FObjectFinder<UStaticMesh>SphereMeshAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
if (SphereMeshAsset.Succeeded())
{
mesh->SetStaticMesh(SphereMeshAsset.Object);
mesh->AddRelativeLocation(FVector(0.f, 0.f, 0.f));
mesh->SetWorldScale3D(FVector(0.2f));
}
/*mesh->SetEnableGravity(true);
mesh->SetSimulatePhysics(true);
mesh->SetGenerateOverlapEvents(true);*/
mesh->SetupAttachment(RootComponent);
InitialLifeSpan = 5.f;
}
【问题讨论】:
标签: c++ unreal-engine4