0

I want to Calculate the Movement Angle an NOT the Rotation from an Moving Actor (to be specific = from a Bullet). First of, I´ve got the Location of the Actor and the Location one Frame before, then calculating the distance between them with FVector::Dist... After that I got the height difference between the current and last Location. Then the A-Tangents from these two variables. I put all these Variables into a Raycast.

When I shoot the Actor forwards it looks good;

Straight

but when I shoot the Bullet up, the Raycast gets messed up:

UP

CODE: `void ABullet::Tick(float DeltaTime) { Super::Tick(DeltaTime);

if (Counter == true)
{
    FVector TraceLoc = GetActorLocation();
    FRotator TraceRot = GetActorRotation() + FRotator(0.f, 90.f, 0.f);

    float Distance = FVector::Dist(LastActorLoc, TraceLoc);
    heightdiff = TraceLoc.Z - LastActorLoc.Z;
    TraceRot.Pitch = (atan(heightdiff / Distance)) * (180.f / 3.141592f);
    UE_LOG(LogTemp, Warning, TEXT("Out: %f"), TraceRot.Pitch);

    FVector Start = TraceLoc;
    FVector End = Start + (TraceRot.Vector() * Distance);

    LastActorLoc = TraceLoc;
    LastActorRot = TraceRot;

    FCollisionQueryParams TraceParamsBullet;
    bool bHitBullet = GetWorld()->LineTraceSingleByChannel(TraceHitResultBullet, Start, End, ECC_Visibility, TraceParamsBullet);
    DrawDebugLine(GetWorld(), Start, End, FColor::Red, false, 15.f, 30.f);
}
if (Counter == false)
{
    Counter = true;
    LastActorLoc = GetActorLocation();
    LastActorRot = GetActorRotation() + FRotator(0.f, 90.f, 0.f);
}

}`

scune
  • 1
  • 1
  • 1
    Probably want to simplify this a bit, you can just do something like `FRotator TraceRot = (TraceLoc - LastActorLoc).GetSafeNormal().ToOrientationRotator();`. But actually the truth is that the line trace doesn't even need a rotator to work so unless it's needed for something else you can simply do `FVector TravelDir = (TraceLoc - LastActorLoc).GetSafeNormal(); FVector Start = GetActorLocation(); FVector End = Start + TravelDir * Distance;`. You could probably simplify this even further by using an overlapping sphere for the projectile. – George Jan 28 '22 at 21:01
  • Thank you very much this worked! – scune Jan 28 '22 at 21:56

0 Answers0