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 );
}