Download

Projectile doesnt hitting objects

**Hey theere , i am faceing a bit of problem ,I got a projectile actor and i want any time the projectile mesh hits something , to print out message .
1)first of all OnHit() is not printing anything ,
2)and the second thing if you notice i have set to BlockAll and the mesh just going through everything no matter what collision preset i set **

Can someone help please , i am really stuck ?



#include "Projectile.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include"Components/StaticMeshComponent.h"

// Sets default values
AProjectile::AProjectile()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;

ProjectileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Projectile Mesh"));

RootComponent = ProjectileMesh;


// Use this component to drive this projectile's movement.
ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovementComponent"));
ProjectileMovementComponent->InitialSpeed = ProjectileSpeed;
ProjectileMovementComponent->MaxSpeed = ProjectileSpeed;



}

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


ProjectileMesh->BodyInstance.SetCollisionProfileName("BlockAll");
ProjectileMesh->SetGenerateOverlapEvents(true);

}

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

ProjectileMesh->OnComponentHit.AddDynamic(this,&AProjectile::OnHit);

}

void AProjectile::FireInDirection(const FVector& ShootDirection)
{
//gives a velocity to the projectile with appropirate Shot Direction
ProjectileMovementComponent->Velocity = ShootDirection * ProjectileMovementComponent->InitialSpeed;
}

void AProjectile::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
//doesnt working somehow
UE_LOG(LogTemp, Warning, TEXT("Hitted"));

}

The possible issues are already in red:

First:

Remove ProjectileMesh->OnComponentHit.AddDynamic(this,&AProjectile::OnHit); from the *Tick *function and put it in the BeginPlay.

Second:

What is inside BeginPlay, put it in the constructor.

Also, try:



ProjectileMesh->BodyInstance.SetCollisionProfileName("Projectile");


And remove ProjectileMesh->SetGenerateOverlapEvents(true);

Third:

*OnHit *must be a UFUNCTION.

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

**Hey there dude , i changed everything as you said , but still nothing changed (((
now my code looks like this **

#include “Projectile.h”
#include “GameFramework/ProjectileMovementComponent.h”
#include"Components/StaticMeshComponent.h"

// Sets default values
AProjectile::AProjectile()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = false;

ProjectileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT(“Projectile Mesh”));

RootComponent = ProjectileMesh;

// Use this component to drive this projectile’s movement.
ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT(“ProjectileMovementComponent”));
ProjectileMovementComponent->InitialSpeed = ProjectileSpeed;
ProjectileMovementComponent->MaxSpeed = ProjectileSpeed;

ProjectileMesh->BodyInstance.SetCollisionProfileName(“Projectile”);

}

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

ProjectileMesh->OnComponentHit.AddDynamic(this, &AProjectile::OnHit);

}

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

}

void AProjectile::FireInDirection(const FVector& ShootDirection)
{
//gives a velocity to the projectile with appropirate Shot Direction
ProjectileMovementComponent->Velocity = ShootDirection * ProjectileMovementComponent->InitialSpeed;
}

void AProjectile::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
//doesnt working somehow
UE_LOG(LogTemp, Warning, TEXT(“Hitted”));

}

Does the staticmesh have collisions? Also, what are you shooting at? And OnHit is an UFUNCTION(), yes?

Best thing you can do, is check how the c++ first person template does it.

Thank you very much , you are amazing!!! i noticed that Projectile’s static mesh didn’t has a Collision . So i turned it on , hit works cool . However now my projectile doesn’t flies and just stays at Spawn point . Any ideas why? )))