Oh I finally got that. So the intersection point between the yellow and red line is always the same. I got it.
Okay now that I got that, I can go further.
This is the code I made, following my logic AND your suggestions
FVector CameraLoc;
FRotator CameraRot;
MyOwner->GetActorEyesViewPoint(CameraLoc, CameraRot);
FHitResult Hit;
GetWorld()->LineTraceSingleByChannel(Hit, CameraLoc, CameraLoc + (CameraRot.Vector() * 9000), COLLISION_WEAPON);
FVector AimDir;
if (Hit.bBlockingHit)
{
AimDir = Hit.Location - MyOwner->GetMesh()->GetSocketLocation((FName("WeaponSocket")));
AimDir.Normalize();
}
else
{
// in this case, AimDir is undefined!
}
FTransform SpawnTM(MyOwner->GetMesh()->GetSocketLocation((FName("WeaponSocket"))));
AProjectile* Projectile = Cast<AProjectile>(UGameplayStatics::BeginDeferredActorSpawnFromClass(this, ProjectileClass, SpawnTM));
if (Projectile)
{
Projectile->Instigator = Instigator;
Projectile->SetOwner(this);
Projectile->InitVelocity(AimDir);
UGameplayStatics::FinishSpawningActor(Projectile, SpawnTM);
}
**
What’s the problem with this code?**
#1
The projectile doesn’t go forward. It goes backwards, in the opposite direction rather than where I aimed!
Yes, I made sure I shot something within 9000 centimeters…
#2 - (this is secondary for now, the bug num#1 is the most intimidating for now!)
AimDir, as you said, CAN be undefined if nothing is hit by the LineTrace. How do I make my projectile start and go towards my crosshair, even if I aimed at the sky?