既然如此,这几个文件都看看
先看看FirstPersonProjectile头文件
定义了几个函数
然后是两个component
这个projectilemovement应该是控制物理运动的
看看CPP文件
sphere那个就创建了一个subobject,初始化了一下,然后这里
CollisionComp->OnComponentHit.AddDynamic(this, &AFirstPersonProjectile::OnHit); // set up a notification for when this component hits something blocking
看起来是将触发函数绑定到这个hit事件上
然后他就是rootComponent了
然后是另外一个component,就是子弹那个
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileComp"));
ProjectileMovement->UpdatedComponent = CollisionComp;
ProjectileMovement->InitialSpeed = 3000.f;
ProjectileMovement->MaxSpeed = 3000.f;
ProjectileMovement->bRotationFollowsVelocity = true;
ProjectileMovement->bShouldBounce = true;
这几个赋值操作看起来分别
- 是指定了他更新谁
- 发射速度
- 最大速度
- 是否考虑旋转
- 反弹
完整构造函数
AFirstPersonProjectile::AFirstPersonProjectile()
{// Use a sphere as a simple collision representationCollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComp"));CollisionComp->InitSphereRadius(5.0f);CollisionComp->BodyInstance.SetCollisionProfileName("Projectile");CollisionComp->OnComponentHit.AddDynamic(this, &AFirstPersonProjectile::OnHit); // set up a notification for when this component hits something blocking// Players can't walk on itCollisionComp->SetWalkableSlopeOverride(FWalkableSlopeOverride(WalkableSlope_Unwalkable, 0.f));CollisionComp->CanCharacterStepUpOn = ECB_No;// Set as root componentRootComponent = CollisionComp;// Use a ProjectileMovementComponent to govern this projectile's movementProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileComp"));ProjectileMovement->UpdatedComponent = CollisionComp;ProjectileMovement->InitialSpeed = 3000.f;ProjectileMovement->MaxSpeed = 3000.f;ProjectileMovement->bRotationFollowsVelocity = true;ProjectileMovement->bShouldBounce = true;// Die after 3 seconds by defaultInitialLifeSpan = 3.0f;
}
另外就是onHit函数实现
如果碰到啥了,且那个对象也进行物理模拟,就给那个对象一个刺激?脉冲?动力?emmm反正就是让他也受力,传过去的是自己的速度*100和自己的位置
void AFirstPersonProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{// Only add impulse and destroy projectile if we hit a physicsif ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr) && OtherComp->IsSimulatingPhysics()){OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation());Destroy();}
}