I made a character and a projectile. When a projectile is created, it remains in place.I create 2d game.
I would appreciate your help.
Character.h
[SPOILER]
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = ProjectileProperties)
UClass* BP_Projectile2;
//create Bullet
void Bullet();
//“Gun”
UPROPERTY(EditAnywhere)
class USkeletalMeshComponent* Gun;
//a shell appears here
UPROPERTY(EditAnywhere)
class USceneComponent* MuzzLoc;
[/SPOILER]
void Bullet()
[SPOILER]
void AMyPaperCharacter1::Bullet()
{
const FVector SpawnLocation = GetActorLocation() + FVector(0.0f, 0.0f, 18.0f);
FActorSpawnParameters ActorSpawnParams;
ActorSpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding;
AMyPaperProjectile2* Projectile = GetWorld()->SpawnActor<AMyPaperProjectile2>(BP_Projectile2, SpawnLocation,
GetActorRotation());
}
[/SPOILER]
Projectile.h
[SPOILER]
#include “CoreMinimal.h”
#include “PaperSpriteActor.h”
#include “Components/SphereComponent.h”
#include “UObject/UObjectGlobals.h”
#include “PaperSpriteComponent.h”
#include “GameFramework/ProjectileMovementComponent.h”
#include “Monster2.h”
#include “MyPaperProjectile2.generated.h”
/**
*
*/
UCLASS()
class TEST1_API AMyPaperProjectile2 : public APaperSpriteActor
{
GENERATED_BODY()
public:
AMyPaperProjectile2();
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Damage)
float Damage;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Damage)
class USphereComponent* ProxSphere;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Damage)
class UPaperSpriteComponent* Sprite;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Movement)
class UProjectileMovementComponent* ProjectileMovement;
FORCEINLINE class UProjectileMovementComponent* GetProjectileMovement() const { return ProjectileMovement; }
UFUNCTION()
void OnCompHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
};
[/SPOILER]
Projectile.cpp
[SPOILER]
// Fill out your copyright notice in the Description page of Project Settings.
#include “MyPaperProjectile2.h”
AMyPaperProjectile2::AMyPaperProjectile2()
{
Sprite = CreateDefaultSubobject<UPaperSpriteComponent>(TEXT(“Sprite”));
ProxSphere = CreateDefaultSubobject<USphereComponent>(TEXT(“ProxSphere”));
ProxSphere->BodyInstance.SetCollisionProfileName(TEXT(“Projectile”));
ProxSphere->OnComponentHit.AddDynamic(this, &AMyPaperProjectile2::OnCompHit);
ProxSphere->SetWalkableSlopeOverride(FWalkableSlopeOverride(WalkableSlope_Unwalkable, 0.0f));
ProxSphere->CanCharacterStepUpOn = ECB_No;
Sprite->AttachTo(ProxSphere);
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT(“ProjectileComp”));
ProjectileMovement->UpdatedComponent = ProxSphere;
ProjectileMovement->InitialSpeed = 1500.0f;
ProjectileMovement->MaxSpeed = 1500.0f;
ProjectileMovement->bShouldBounce = true;
ProjectileMovement->ProjectileGravityScale = 0.0f;
InitialLifeSpan = 3.3f;
}
void AMyPaperProjectile2::OnCompHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL))
{
OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation());
if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT(“I Hit: %s”), *OtherActor->GetName()));
}
}
[/SPOILER]