Hi all. Following the tutorial on this page
I think, I’ve followed everything, but I clearly must have done something wrong because my projectiles don’t collide at all.
Here is what I have so far.
Header file
#pragma once
#include "GameFramework/Actor.h"
#include "ROMProjectile.generated.h"
/**
*
*/
UCLASS()
class ROM_API AROMProjectile : public AActor
{
GENERATED_UCLASS_BODY()
/** Sphere collision component */
UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
TSubobjectPtr<USphereComponent> CollisionComp;
/** Projectile movement component */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement)
TSubobjectPtr<class UProjectileMovementComponent> ProjectileMovement;
/** inits 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);
};
Here is the cpp file.
#include "ROM.h"
#include "ROMProjectile.h"
AROMProjectile::AROMProjectile(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
// Use a sphere as a simple collision representation
CollisionComp = PCIP.CreateDefaultSubobject<USphereComponent>(this, TEXT("SphereComp"));
CollisionComp->InitSphereRadius(15.0f);
RootComponent = CollisionComp;
CollisionComp->BodyInstance.SetCollisionProfileName("Projectile");
CollisionComp->OnComponentHit.AddDynamic(this, &AROMProjectile::OnHit);
// Use a ProjectileMovementComponent to govern this projectile's movement
ProjectileMovement = PCIP.CreateDefaultSubobject<UProjectileMovementComponent>(this, TEXT("ProjectileComp"));
ProjectileMovement->UpdatedComponent = CollisionComp;
ProjectileMovement->InitialSpeed = 3000.f;
ProjectileMovement->MaxSpeed = 3000.f;
ProjectileMovement->bRotationFollowsVelocity = true;
ProjectileMovement->bShouldBounce = true;
ProjectileMovement->Bounciness = 0.3f;
// Die after 5 seconds by default
InitialLifeSpan = 5.0f;
}
void AROMProjectile::InitVelocity(const FVector& ShootDirection)
{
if (ProjectileMovement)
{
// set the projectile's velocity to the desired direction
ProjectileMovement->Velocity = ShootDirection * ProjectileMovement->InitialSpeed;
}
}
void AROMProjectile::OnHit(AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
if (OtherActor && (OtherActor != this) && OtherComp)
{
OtherComp->AddImpulseAtLocation(ProjectileMovement->Velocity * 100.0f, Hit.ImpactPoint);
}
}
Any ideas where I’ve gone wrong?
Thanks in advance everyone! Any help is very much appreciated.