3.3 - Setting up Projectile Collision and Lifetime
Currently, our projectiles:
Live forever (they never disappear from the Scene Outliner)
Don't collide with other objects in the world
During this step, we're going to set up projectile collision and lifetime.
Limiting the Projectile's Life Span
Locate the
FPSProjectile
class CPP file in the Solution Explorer and openFPSProjectile.cpp
.Add the following code to the
FPSProjectile
constructor to set the projectile's lifespan:// Die after 3 seconds. InitialLifeSpan = 3.0f;
Editing the Projectile's Collision Settings
Unreal Engine comes packaged with several preset collision chanels; however, the engine also provides customizable channels that game projects can use.
Open Project Settings and select Collision to customize a collision channel.
Select New Object Channel... to create a new collision channel. Name your new collision channel "Projectile" and make sure that the Default Response is set to Block before clicking Accept.
Select New... under Preset and name your new profile "Projectile" as well. Refer to the following image to set your collision presets.
This collision profile specifies that the projectile will be blocked by
Static Actors
,Dynamic Actors
,Actors
simulatingPhysics
,Vehicles
, andDestructible Actors
. Also, this collision profile specifies that the projectile overlapsPawns
.
Using the New Collision Channel's Settings
Locate the
FPSProjectile
class CPP file in the Solution Explorer and openFPSProjectile.cpp
.In the
FPSProjectile
constructor, add the following line after the creation ofCollisionComponent
:CollisionComponent->BodyInstance.SetCollisionProfileName(TEXT("Projectile"));
FPSProjectile.cpp
should now look like the following:// Fill out your copyright notice in the Description page of Project Settings. #include "FPSProject.h" #include "FPSProjectile.h" // Sets default values AFPSProjectile::AFPSProjectile() { // 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; // Use a sphere as a simple collision representation. CollisionComponent = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent")); CollisionComponent->BodyInstance.SetCollisionProfileName(TEXT("Projectile")); // Set the sphere's collision radius. CollisionComponent->InitSphereRadius(15.0f); // Set the root component to be the collision component. RootComponent = CollisionComponent; // Use this component to drive this projectile's movement. ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovementComponent")); ProjectileMovementComponent->SetUpdatedComponent(CollisionComponent); ProjectileMovementComponent->InitialSpeed = 3000.0f; ProjectileMovementComponent->MaxSpeed = 3000.0f; ProjectileMovementComponent->bRotationFollowsVelocity = true; ProjectileMovementComponent->bShouldBounce = true; ProjectileMovementComponent->Bounciness = 0.3f; // Die after 3 seconds. InitialLifeSpan = 3.0f; } // Called when the game starts or when spawned void AFPSProjectile::BeginPlay() { Super::BeginPlay(); } // Called every frame void AFPSProjectile::Tick( float DeltaTime ) { Super::Tick( DeltaTime ); } // Function that initializes the projectile's velocity in the shoot direction. void AFPSProjectile::FireInDirection(const FVector& ShootDirection) { ProjectileMovementComponent->Velocity = ShootDirection * ProjectileMovementComponent->InitialSpeed; }
Save
FPSProjectile.cpp
in Visual Studio.Locate FPSProject in the Solution Explorer.
Right-click on FPSProject and select Build to compile your project.