3.3 - Setting up Projectile Collision and Lifetime

Learn how to set up collision and lifetime for your First Person Shooter projectiles.

Windows
MacOS
Linux

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

  1. Locate the FPSProjectile class CPP file in the Solution Explorer and open FPSProjectile.cpp.

  2. 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.

  1. Open Project Settings and select Collision to customize a collision channel.

    SettingCollisionChannels.png

  2. 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.

    NewChannel.png

  3. Select New... under Preset and name your new profile "Projectile" as well. Refer to the following image to set your collision presets.

    NewProfile.png

    This collision profile specifies that the projectile will be blocked by Static Actors, Dynamic Actors, Actors simulating Physics, Vehicles, and Destructible Actors. Also, this collision profile specifies that the projectile overlaps Pawns.

Using the New Collision Channel's Settings

  1. Locate the FPSProjectile class CPP file in the Solution Explorer and open FPSProjectile.cpp.

  2. In the FPSProjectile constructor, add the following line after the creation of CollisionComponent:

    CollisionComponent->BodyInstance.SetCollisionProfileName(TEXT("Projectile"));
  3. 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;
    }
  4. Save FPSProjectile.cpp in Visual Studio.

  5. Locate FPSProject in the Solution Explorer.

  6. Right-click on FPSProject and select Build to compile your project.

    BuildFPSProject.png

Select Skin
Light
Dark

Welcome to the new Unreal Engine 4 Documentation site!

We're working on lots of new features including a feedback system so you can tell us how we are doing. It's not quite ready for use in the wild yet, so head over to the Documentation Feedback forum to tell us about this page or call out any issues you are encountering in the meantime.

We'll be sure to let you know when the new system is up and running.

Post Feedback