Issues with MovementSpeed

For some reason the movement speed of the projectile is (but also isn’t) changing. I’ll explain. Basically I initially noticed that whatever I changed Movement Speed to in the BP changed nothing, it kept firing out in front of me. I then went to the projectile movement component and saw that both the Initial and Max speed were both set to 0, which I thought was strange as I had used the pointers from the lecture. So I manually changed them to 1300 and the projectile started launching as expected. And I thought well I want to change both by changing the movement speed, that was the point of the pointers (no pun intended) so I’ll try changing movement speed and see what happens. I changed it to less than 1300, works as intended. Okay, great. Try changing it to above 1300 (say 2500). Still launches as if it was set at 1300. Aha, so movement speed doesn’t alter anything unless it MovementSpeed <= Initial/Max Speed, which I don’t believe was the intention here? I don’t think I’m missing anything. But here’s my code.

ProjectileBase.h:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ProjectileBase.generated.h"

class UProjectileMovementComponent;
UCLASS()
class TOONTANKS_API AProjectileBase : public AActor
{
	GENERATED_BODY()

private:
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
	UProjectileMovementComponent* ProjectileMovement;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
	UStaticMeshComponent* ProjectileMesh;
	UPROPERTY(EditDefaultsOnly, Category = "Damage")
	TSubclassOf<UDamageType> DamageType;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Move", meta = (AllowPrivateAccess = "true"))
	float MovementSpeed = 1300.f;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Damage", meta = (AllowPrivateAccess = "true"))
	float Damage = 50.f;

public:	
	// Sets default values for this actor's properties
	AProjectileBase();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

};

ProjectileBase.cpp:

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

// Sets default values
AProjectileBase::AProjectileBase()
{
 	// 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;

	ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("Projectile Movement"));
	
}

// Called when the game starts or when spawned
void AProjectileBase::BeginPlay()
{
	Super::BeginPlay();
	ProjectileMovement->InitialSpeed = MovementSpeed;
	ProjectileMovement->MaxSpeed = MovementSpeed;
	InitialLifeSpan = 3.f;
}

My spawn points are 100 units out from the turret, and I’ve tried moving the code under BeginPlay back to the contructor and that didn’t change anything either.

I’m having the exact same problem with the exact same code that you have written, sitting on UE version 4.26.2

I should add that initial life span does not work from code either, my projectiles only despawn if I change the value inside the blueprint.

Edit: I put the initial lifespan BEFORE the super, and now they disappear after the time I set in code, the Movement speed still misbehaves as fonkvan describes it no matter if it’s before or after the super

Sorry about this guys. I alerted Rob to the problem of setting the values of these in the constructor (won’t update already made blueprints/instances) a while back and then a bit after creating the ticket was made aware of the issue in this thread but didn’t update the ticket.

The solutions pointed out here should work

2 Likes

Thank you! Moving the things into PostActorCreated() did the trick!

Thank you! I also had success using the PostActorCreated method.

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

Privacy & Terms