Calculating a Vector for Shooting

I am having trouble calculating the correct vector for my shoot mechanic. Here I initialize all the variables I will need to calculate the final vector. The problem is GetAdjustedAim always returns a Zero’ed out Vector.

void AArenaRangedWeapon_Instant::FireWeapon()
{
    (...)
	const FVector AimDir = GetAdjustedAim();
	const FVector StartTrace = GetCameraDamageStartLocation(AimDir);
	const FVector ShootDir = WeaponRandomStream.VRandCone(AimDir, ConeHalfAngle, ConeHalfAngle);
	const FVector EndTrace = StartTrace + ShootDir * InstantConfig.WeaponRange;
    (...)
}

Here is where I calculate the GetAdjustedAim. I get the PlayerController for the player and get the camLoc and the camRot. I zero out the vector so my result each iteration of FireWeapon() will be clean. However both the IF and the ELSE IF statement are always false. There must be something I don’t understand about the controller class.

FVector AArenaRangedWeapon::GetAdjustedAim() const
{
	AArenaPlayerController* PlayerController = Instigator ? Cast<AArenaPlayerController>(Instigator->Controller) : NULL;
	FVector FinalAim = FVector::ZeroVector;
	// If we have a player controller use it for the aim
	if (PlayerController)
	{
		FVector CamLoc;
		FRotator CamRot;
		PlayerController->GetPlayerViewPoint(CamLoc, CamRot);
		FinalAim = CamRot.Vector();
	}
	else if (Instigator)
	{
		// Now see if we have an AI controller - we will want to get the aim from there if we do
		AArenaAIController* AIController = MyPawn ? Cast<AArenaAIController>(MyPawn->Controller) : NULL;
		if (AIController != NULL)
		{
			FinalAim = AIController->GetControlRotation().Vector();
		}
		else
		{
			FinalAim = Instigator->GetBaseAimRotation().Vector();
		}
	}

	return FinalAim;
}

Here is where I initialize the Instigator and the MyPawn so that they are valid.

void AArenaRangedWeapon::SetOwningPawn(ATheArenaCharacter* NewOwner)
{
	if (MyPawn != NewOwner)
	{
		Instigator = NewOwner;
		MyPawn = NewOwner;
		// net owner for RPC calls
		SetOwner(NewOwner);
	}
}

If anyone could take a look at this and give me some ideas as to what is going on they would be a real champion!

If it returns zero, i would assume that the if and the else if are both FALSE.

Would you mind checking the execution of these parts with a debug message?

I don’t know what causes the zero at the moment, but testing the ifs first would be good.

Sorry I should have been more clear. Yes in my testing IF and ELSE IF were both false. I don’t understand why though, I don’t know much about how the controller classes work. I’m assuming there is something about them that I am misunderstanding.

So you are getting the controller over the instigator.

If the Instigator is Null, the Controller will be Null, so i guess the problem is the instigator.

Could you check if the “SetOwningPawn” function calls the if?

Because if MyPawn == NewOwner is TRUE, you wont have Instigator set.

Please check this and try to set Instigator even if MyPawn != NewOwner is FALSE. (:

These are just some debugging thoughts :smiley: I’m also not that good in C++ and controllers.

Do you SetOwningPawn(this) in your Character SetCurrentWeapon?

I fixed it. I just removed all the implementations the player controller like so:

FVector AArenaRangedWeapon::GetAdjustedAim() const
{
	FVector FinalAim = FVector::ZeroVector;
	
	FinalAim = Instigator->GetBaseAimRotation().Vector();
	return FinalAim;
}

This may have issues if we scale the game, but for now it works perfectly