Error with ProjectileMovement->Velocity in FPS Tutorial

Hey guys, I’m currently working on the FPS tutorial on their wiki page. I’m on the part dealing w/ how to deal when colliding in physics. It has me create an OnHit function, and inside that inside an if check, it has me adding an impulse at location function. It wants me to use ProjectileMovement->Velocity * 100.0f as the 1st param. When I try to compile it says that ProjectileMovement is an undeclared variable. It is declared, and I’m using it in the constructor. Anyone know why it’s saying this, and how to fix it?

FPSProjectile.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Actor.h"
#include "FPSProjectile.generated.h"

UCLASS()
class FPSPROJECT_API AFPSProjectile : public AActor
{
	GENERATED_BODY()
	
public:
	// Sets default values for this actor's properties
	AFPSProjectile(const FObjectInitializer& ObjectInitializer);

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	/**Sphere Collision Component **/
	UPROPERTY(VisibleDefaultsOnly, Category = "Projectile")
		USphereComponent* CollisionComponent;

	/* Projectile movement component */
	UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "Projectile")
		UProjectileMovementComponent* ProjectileMovement;

	/* init's velocity of the projectile in the shoot direction */
	void InitVelocity(const FVector& ShootDirection);

	// called when projectile hits something
	UFUNCTION()
		void OnHit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);

};

FPSProjectile.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "FPSProject.h"
#include "FPSProjectile.h"

// Sets default values
AFPSProjectile::AFPSProjectile(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
 	// 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 = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("CollisionComponent"));
	CollisionComponent->InitSphereRadius(15.0f);
	RootComponent = CollisionComponent;
	CollisionComponent->BodyInstance.SetCollisionProfileName("Projectile");
	CollisionComponent->OnComponentHit.AddDynamic(this, &AFPSProjectile::OnHit);

	// Use UProjectileMovementComponent to govern this projectile's movement
	ProjectileMovement = ObjectInitializer.CreateDefaultSubobject<UProjectileMovementComponent>(this, TEXT("ProjectileComponent"));
	ProjectileMovement->UpdatedComponent = CollisionComponent;
	ProjectileMovement->InitialSpeed = 3000.f;
	ProjectileMovement->MaxSpeed = 3000.f;
	ProjectileMovement->bRotationFollowsVelocity = true;
	ProjectileMovement->bShouldBounce = true;
	ProjectileMovement->Bounciness = 0.3f;

	// die after 3 seconds by default
	InitialLifeSpan = 3.0f;

}

void AFPSProjectile::InitVelocity(const FVector& ShootDirection)
{
	if (ProjectileMovement)
	{
		// set the projectile's velocity to the desired direction
		ProjectileMovement->Velocity = ShootDirection * ProjectileMovement->InitialSpeed;
	}
}


void OnHit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
	if (OtherActor /*&& (OtherActor != this)*/ && OtherComp)
	{
		OtherComp->AddImpulseAtLocation(ProjectileMovement->Velocity * 100.0f, Hit.ImpactPoint);
	}
}


// Called when the game starts or when spawned
void AFPSProjectile::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AFPSProjectile::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

}

Are you using 4.7.X ?

If yes, change GENERATE_BODY for GENERATE_UCLASS_BODY and delete

// Sets default values for this actor’s properties
AFPSProjectile(const FObjectInitializer& ObjectInitializer);

from your .h

I’m using 4.7.3. Just tried that, and it is still saying that ProjectileMovement is an undeclared identifier.

Hey hornfreak ,

Sorry about that quick answer I was not on my main PC with UE4.

So I gave a go at your code and changed a few things but I think your main problem comes from your declaration of the OnHit function. It’s not scoped to the projectile class

If you add AFPSProjectile:: before it in your cpp should help you quite a bit.

I also had to add #include “GameFramework/ProjectileMovementComponent.h” to your .h but I find it weird you got that far without the include.

That should do it

If there’s anything else I’ll be glad to help

Cheers

  • Marc

Lol, wow I cannot believe I kept missing that. Thanks Marc, that solved it!

Glad that solved it

don’t forget tonaccept my answer for better tracking

Cheers

-Marc