So I have my projectile actor, which is supposed to fly by ballistic trajectory, so that I place it in the right position with right rotation with code, and then turn on physics. Somewhy it doesn’t appear to be where it’s supposed to be, instead it starts where it was last placed in viewport, even though GetActorLocation() and GetActorRotation() tell me it’s placed correctly. Also in this case physics simulation works, but added impulse doesn’t. If I disable physics simulation, impulse pushes actor a bit forward but then it stops and shows a message telling me that in order to add impulse I need to turn on physics simulation. What do I do?
.h:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SceneComponent.h"
#include "Kismet/KismetMathLibrary.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "GameFramework/PlayerController.h"
#include "TarProjectile_CPP.generated.h"
UCLASS()
class TUI_API ATarProjectile_CPP : public AActor
{
GENERATED_BODY()
public:
ATarProjectile_CPP();
FVector StartPos = FVector(-6690.f, -290.f, 130.f);
FRotator MoveVector = FRotator(60, 0, 0);
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
void SendTarget();
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
class UStaticMeshComponent* Mesh;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Physics")
float weight = 1;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Physics")
float InitialImpulseStrength = 20;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Physics")
class UProjectileMovementComponent* ProjectileMovementComponent;
};
.cpp:
#include "TarProjectile_CPP.h"
// Sets default values
ATarProjectile_CPP::ATarProjectile_CPP()
{
PrimaryActorTick.bCanEverTick = true;
Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh Component"));
RootComponent = Mesh;
static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshAsset(TEXT("/Game/meshes/Missile_AIM-120_D__AMRAAM_.Missile_AIM-120_D__AMRAAM_"));
ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovementComponent"));
ProjectileMovementComponent->SetUpdatedComponent(Mesh);
ProjectileMovementComponent->InitialSpeed = 3000.f;
ProjectileMovementComponent->MaxSpeed = 3000.f;
ProjectileMovementComponent->bShouldBounce = false;
if (MeshAsset.Succeeded())
{
Mesh->SetStaticMesh(MeshAsset.Object);
Mesh->SetWorldScale3D(FVector(1.f));
}
}
// Called when the game starts or when spawned
void ATarProjectile_CPP::BeginPlay()
{
Super::BeginPlay();
SetActorLocation(StartPos);
SetActorRotation(MoveVector);
Mesh->SetMassScale(NAME_None, weight);
Mesh->SetSimulatePhysics(true);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, GetActorLocation().ToString());
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, GetActorRotation().ToString());
FVector LaunchImpulse = GetActorForwardVector() * InitialImpulseStrength;
Mesh->AddImpulse(LaunchImpulse);
}
// Called every frame
void ATarProjectile_CPP::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ATarProjectile_CPP::SendTarget()
{
}