Can't set transform options of StaticMeshComponent

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()
{
	
}


No idea, sorry, I don’t use CPP :melting_face:

Why do it with CPP actually? ( huge number of these things? )

I just prefer using CPP, and I’m new into UE4, so I don’t know where and how I should do it. But thank you for your response!

The trace looks ok, as far as I can tell. But maybe try it in a totally empty level, to avoid any collision problems.

I guess I already solved it. For some reason if I enable physics for it’s children blueprint-class, it does work as it’s supposed to. Perhaps I will have to leave it like this. But thank you for your support!

1 Like

is the physics target the root component, if not the component will move but the root will stay which is what will be returned on getactorlocation()

I already solved it somehow(read some post above) but yes StaticMeshComponent was the RootComponent and the only component of this actor, so that physics were being applied to both RootComponent and MeshComponent at the same time, and still it didn’t work.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.