Projectiles move (after shot) when charater move

I’m using the ProjectileMovement for a lazer gun … small stretched out sphere.

if I stand still they fire in a straight line…

but if I rotate the character all the projectiles start to move … even the ones that were shot a while ago.

I don’t want the projectiles I already shot to change direction
The pics don’t show whats happening as much as I would like … the projectiles start to move down up … all over the speed I have set is 350 so the second pic those projectile were shot 10 seconds earlier and I rotated fast…

Are you spawning the projectiles attached to the player? Might want to show the BP or c++ dealing with spawning the lazers

yes there attached to a arrow component coming out of the gun…

void AWeaponBase::OnShoot_Implementation(UWorld* world, APawn* gunOwner, float attackPower)
{
	AActor* weapon = SpawnBullitBP<AActor>(GetWorld(), BPBullit, ProjectileSpawnPoint->GetComponentLocation()/*+ (ProjectileSpawnPoint->GetForwardVector() * 100)*/, ProjectileSpawnPoint->GetComponentRotation());
	
	AAmmoBase* projectile = Cast<AAmmoBase>(weapon);
	if (projectile != NULL)
	{
		FVector const LaunchDir = ProjectileSpawnPoint->GetForwardVector() * 100;
		// place it at the barrel of the gun
		projectile->AttachToComponent(ProjectileSpawnPoint, FAttachmentTransformRules::SnapToTargetIncludingScale, "");
		projectile->InitStats(_AttackRange, _Damage);
		projectile->InitVelocity(LaunchDir);
	}
}


void AAmmoBase::InitVelocity(const FVector & ShootDirection)
{
	if (ProjectileMovement)
	{
		// set the projectile's velocity to the desired direction
		ProjectileMovement->Velocity = ShootDirection * ProjectileMovement->InitialSpeed;
	}	
}

oh and ProjectileSpawnPoint is the ArrowComponet

FYI in case it matters I was doing this all in Blueprints.
I had the exact same problem. I had the projectile velocity set to the forward vector of the arrow. I fixed it by just setting the projectiles x-axis velocity to the speed I wanted it to travel at. I’m guessing projectile velocity is relative to the projectile and by using the forward vector it added velocity to the projectile’s other axi even though I just wanted it to shoot in the direction I was aiming. And I’m guessing you’re doing the same thing too in line 22. Try taking out ShootDirection from line 22 maybe (or maybe line12). Sorry I’m not a programmer.

I took off attachtocomponet and initial speed 0 and just used velocity.X … it fixed the bullets moving with the character… but now its shooting in the direction the gun is in idle … when the weapon is first spawned in the hands …
When the animation raises his hands it still shoots where the gun was spawned.

AWeaponBase::AWeaponBase()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	_ItemType = EItemType::IE_WEAPON;

	WeaponMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("WeaponMesh"));
	WeaponMesh->AttachToComponent(RootComponent, FAttachmentTransformRules::SnapToTargetIncludingScale, "");

	ProjectileSpawnPoint = CreateDefaultSubobject<UArrowComponent>(TEXT("ProjectileSpawnPoint"));
	ProjectileSpawnPoint->AttachToComponent(WeaponMesh, FAttachmentTransformRules::SnapToTargetIncludingScale, "");
}

and updated the code where I spawn the bullet

void AWeaponBase::OnShoot_Implementation(UWorld* world, APawn* gunOwner, float attackPower)
{
	
	AActor* weapon = SpawnBullitBP<AActor>(GetWorld(), BPBullit, ProjectileSpawnPoint->GetComponentLocation(), ProjectileSpawnPoint->GetComponentRotation());
		
	AAmmoBase* projectile = Cast<AAmmoBase>(weapon);
	if (projectile != NULL)
	{
		//FVector const LaunchDir = ProjectileSpawnPoint->GetForwardVector();
		projectile->InitStats(_AttackRange, _Damage);
		//projectile->InitVelocity(LaunchDir);
	}
}

I created a Blueprint from class … so I could put the arrow at the gun barrel and that’s where I set the x value to 350
Guessing my code isn’t getting the blueprints ProjectileSpawnPoint (Arrow) its using the values from when the weapon object was made … not sure how to get the blueprints arrow location and rotation

1 Like

Is the arrow attached to the weapon? line 4 you have projectile spawn point twice. I dont know if that’s right as I dont program. I haven’t worked much with characters with more than one weapon. Worst case maybe look at the location of your arrow and use that as an offset from your weapon’s location to spawn from? So if in BP your arrow/weapon’s muzzle is at x=100,y=0, Z=0, make that your projectile spawn point offset.