I’m making a grappling hook.
Heres the blueprints I started with:
Heres my attempt at porting those blueprints to code:
// Called every frame
void UGrappleGunComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (m_IsHooked)
{
//if player comes within 400 units of hook, or if player passes 90 degrees from hook normal, detach.
distanceToHook = m_Sphere->GetComponentLocation() - m_HookedLoc;
//if (distanceToHook.Size() < 400 || FVector::DotProduct(m_ImpactNormal, distanceToHook.GetSafeNormal(0.0001)) > 0)
if (distanceToHook.Size() < 400)
{
DetachMethod();
}
else if (FVector::DotProduct(m_ImpactNormal, distanceToHook.GetSafeNormal(0.0001)) >= 0)
{
DetachMethod();
}
//if you have no reason to detach, launch!
else
{
//GetPlayerCharacter
ACharacter* PlayerCharacter = Cast<ACharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
//LaunchVelocity calculation
FVector LaunchVelocity = ((m_HookedLoc - m_Sphere->GetComponentLocation()).GetSafeNormal(0.0001) + m_Character_Aim_Direction->GetForwardVector()) * 10;
//launch character with calculated velocity
m_Character_Movement->GravityScale = 0.33;
PlayerCharacter->LaunchCharacter(LaunchVelocity, false, false);
}
}
}
The blueprints are in BP_GrappleHook, which inherits from GrappleGunComponent, where the code is. As I understand it, this code should work the same as the blueprints, but when I cut the link from the event tick node to the rest of the functionality and let the code do the work instead, the 90 degree check stops working. I may be misunderstanding the maths but I’m pretty sure the calculation is identical in both instances.