i have two types of arrows which are defined and spawned in character controller as follows:
character.h file:
UPROPERTY(EditAnywhere,BlueprintReadWrite, Category = Projectile)
TSubclassOf<class AArrowProjectile> NormalArrowClass;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Projectile)
TSubclassOf<class ASpeicalArrow> SpecialArrowClass;
Character.cpp file:
switch (SelectedArrowNumber){
case 0:
World->SpawnActor<AArrowProjectile>(NormalArrowClass, SpawnLocation, SpawnRotation, ActorSpawnParams);
break;
case 1:
World->SpawnActor<ASpeicalArrow>(SpecialArrowClass, SpawnLocation, SpawnRotation, ActorSpawnParams);
break;
default:
World->SpawnActor<AArrowProjectile>(NormalArrowClass, SpawnLocation, SpawnRotation, ActorSpawnParams);
break;
}
each one has a separate actor type class where i have defined a static mesh and a sphere collider for them
im trying to activate hit even for them but only the first one is activating
ArrowProjectile.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ArrowProjectile.generated.h"
UCLASS()
class TESTSHOOTER_API AArrowProjectile : public AActor
{
GENERATED_BODY()
UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
class USphereComponent* CollisionComp;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Movement, meta = (AllowPrivateAccess = "true"))
class UProjectileMovementComponent* ProjectileMovement;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Mesh, meta = (AllowPrivateAccess = "true"))
class UStaticMeshComponent* ArrowMesh;
UPROPERTY(EditDefaultsOnly, Category = Default)
AActor* PlayerActorBlueprint;
public:
// Sets default values for this actor's properties
AArrowProjectile();
protected:
UFUNCTION()
void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
public:
/** Returns CollisionComp subobject **/
FORCEINLINE class USphereComponent* GetCollisionComp() const { return CollisionComp; }
/** Returns ProjectileMovement subobject **/
FORCEINLINE class UProjectileMovementComponent* GetProjectileMovement() const { return ProjectileMovement; }
};
ArrowProjectile.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "ArrowProjectile.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Components/SphereComponent.h"
// Sets default values
AArrowProjectile::AArrowProjectile()
{
// Use a sphere as a simple collision representation
CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComp"));
CollisionComp->InitSphereRadius(5.0f);
// Players can't walk on it
CollisionComp->SetWalkableSlopeOverride(FWalkableSlopeOverride(WalkableSlope_Unwalkable, 0.f));
CollisionComp->CanCharacterStepUpOn = ECB_No;
CollisionComp->OnComponentHit.AddDynamic(this, &AArrowProjectile::OnHit);
ArrowMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Arrow Mesh"));
ArrowMesh->SetupAttachment(CollisionComp);
// Set as root component
RootComponent = CollisionComp;
// Use a ProjectileMovementComponent to govern this projectile's movement
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileComp"));
ProjectileMovement->UpdatedComponent = CollisionComp;
ProjectileMovement->InitialSpeed = 5000/1.2f;
ProjectileMovement->MaxSpeed = 5000*1.2f;
ProjectileMovement->bRotationFollowsVelocity = true;
ProjectileMovement->bShouldBounce = false;
// Die after 3 seconds by default
InitialLifeSpan = 3.0f;
}
void AArrowProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Red, TEXT("Normal Arrow Hit"));
// Only add impulse and destroy projectile if we hit a physics
if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL) && OtherComp->IsSimulatingPhysics())
{
OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation());
Destroy();
}
}
the SpeicalArrow class uses exactly the same code the only difference is wherever there is ArrowProjectile or AArrowProjectile i changed it to SpeicalArrow or ASpecialArrow and changed the Gengine->… messages
so when i fire the normal arrow i get the “Normal arrow spawned” and “Normal Arrow Hit” message
but when i fire second arrow i only get “Special Arrow Spawned” message so the OnHit(…) function is not running.
both arrow are using the same collision preset and object type