Download

TPS Projectile off from cursor

Hello there, its going to be my first forum post here.
Im learning C++ and UE4 with the Survival template and im on my way to finish the projectile based weapon class. The only problem is that is slightly away or sometimes behind the place where im aiming. Any tips?
Here a picture and the code too.

The cpp file:



#include "SquadKill.h"
#include "SWeaponProjectile.h"
#include "SExplosionEffect.h"
#include "SPlayerController.h"
#include "SDamageType.h"

ASWeaponProjectile::ASWeaponProjectile(const class FObjectInitializer& PCIP)
	: Super(PCIP)
{
}

void ASWeaponProjectile::FireWeapon()
{
	const FVector AimDir = GetAdjustedAim();
	const FVector CameraPos = GetCameraDamageStartLocation(AimDir);
	const FVector EndPos = CameraPos + (AimDir * 15000.0f);

	/* Check for impact by tracing from the camera position */
	FHitResult Impact = WeaponTrace(CameraPos, EndPos);

	const FVector MuzzleOrigin = GetMuzzleLocation();

	FVector AdjustedAimDir = AimDir;
	if (Impact.bBlockingHit)
	{
		/* Adjust the shoot direction to hit at the crosshair. */
		AdjustedAimDir = (Impact.ImpactPoint - MuzzleOrigin).GetSafeNormal();

		/* Re-trace with the new aim direction coming out of the weapon muzzle */
		Impact = WeaponTrace(MuzzleOrigin, MuzzleOrigin + (AdjustedAimDir * 15000.0f));
	}
	else
	{
		/* Use the maximum distance as the adjust direction */
		Impact.ImpactPoint = FVector_NetQuantize(EndPos);
	}
	ServerFireProjectile(MuzzleOrigin, AimDir);
}

bool ASWeaponProjectile::ServerFireProjectile_Validate(FVector Origin, FVector ShootDir)
{
	return true;
}

void ASWeaponProjectile::ServerFireProjectile_Implementation(FVector Origin, FVector ShootDir)
{
	FTransform SpawnTM(ShootDir.Rotation(), Origin);
	ASProjectile* Projectile = Cast<ASProjectile>(UGameplayStatics::BeginDeferredActorSpawnFromClass(this, ProjectileClass, SpawnTM));
	if (Projectile)
	{
		Projectile->Instigator = Instigator;
		Projectile->SetOwner(this);
		Projectile->InitVelocity(ShootDir);

		UGameplayStatics::FinishSpawningActor(Projectile, SpawnTM);
	}
}


The Header File



#pragma once

#include "SWeapon.h"
#include "SWeaponProjectile.generated.h"

/**
 * 
 */
UCLASS(Abstract)
class SQUADKILL_API ASWeaponProjectile : public ASWeapon
{
	GENERATED_BODY()

	UPROPERTY(EditDefaultsOnly, Category = "Projectile")
	TSubclassOf<class ASProjectile> ProjectileClass;

protected:
	ASWeaponProjectile(const FObjectInitializer& ObjectInitializer);

	virtual void FireWeapon() override;

	/** spawn projectile on server */
	UFUNCTION(reliable, server, WithValidation)
	void ServerFireProjectile(FVector Origin, FVector ShootDir);

	void ServerFireProjectile_Implementation(FVector Origin, FVector ShootDir);

	bool ServerFireProjectile_Validate(FVector Origin, FVector ShootDir);
};