Projectile Collision

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.

Hi Thorax,

what do you mean by “not colliding”? Is the OnHit Method ever being called? If it is not being called, you should check if the collision settings are correct! (Open the Blueprint → Components → Mark your colliding box/sphere and check if generate Hit events is checked and if its Collision Channels are set correctly!)

[You might check out: Collision in Unreal Engine | Unreal Engine Documentation]

Good luck!

Nano

Sorry, by colliding I meant the sphere projectile wasnt bouncing off of objects.

I had enabled collision on the projectile mesh, as opposed to the CollisionComp
This is how they are set now and that seems to have fixed the collision issue.

However… For some reason now, the projectiles only shoot in one direction. If i turn or look up and down they always shoot along the X axis in a positive direction. No idea what that could be caused by.

Does your code match the one in the tutorial? Can you post your Fire-Method here? (OnFire?)

As far as I’m aware everything matches up. Unless I’ve made a mistake somewhere.

This is the OnFire method from the ROMCharacter.cpp

EDIT: Code didnt display well on here, switched to pastebin

Thanks again!