How to make a Spider in UE4 Part 6

In this post add convex transitions to the spider. Basically up to this point the spider can navigate inside, or concave corners but can’t go around outside ones. The code to do this ended up being minimal.

 

I added another socket on the spider, to get the normal of the convex surface from a trace from a socket in front of the body.

ConvexCheck

 

In the tick, check to see if there is a surface under the front legs, if there’s not the get the normal of the surface that is convex to the current surface it’s on.

 

            //Check if convex corner
            if (!GetWorld()->LineTraceSingle(HitData, DirectionSocketLocation, FLSocketLoc, ECollisionChannel::ECC_Pawn, TraceParams) && 
                !GetWorld()->LineTraceSingle(HitData2, DirectionSocketLocation, FRSocketLoc, ECollisionChannel::ECC_Pawn, TraceParams))
            {
                HitNormal = HitData.ImpactNormal;
                HitNormal2 = HitData2.ImpactNormal;

                if (!HitData.GetActor() && !HitData2.GetActor())
                {
                    DidTransition = TransitionToWall(true);
                }
            }
            //Point directly in front
            else if (GetWorld()->LineTraceSingle(HitData, BellySocketLoc, FrontSocketLoc, ECollisionChannel::ECC_Pawn, TraceParams))
            {
                HitNormal = HitData.ImpactNormal;
                if (HitData.GetActor())
                {
                    DidTransition = TransitionToWall(false);
                }
            }

To transition to the new surface, need to know how to do the trace as it’s different depending on which way.

bool ASpiderPawn::TransitionToWall(bool IsConvex)
{
    bool retval = false;

    //Grab a ref to the world here
    UWorld* world = GetWorld();

    FHitResult hitResultTrace;
    FCollisionQueryParams queryParams;
    FCollisionObjectQueryParams objQueryParams;

    queryParams.AddIgnoredActor(this);

    FVector Start, End;

    FVector under;

    FVector newUp;
    FVector newForward;
    FVector newRight;

    if (!IsConvex)
    {
        Start = BellySocketLoc;
        End = FrontSocketLoc;
    }
    else
    {
        Start = FrontSocketLoc;
        End = ConvexCheckLoc;
    }

    //Trace to get the surface normal in front of actor
    if (world->LineTraceSingle(hitResultTrace, Start, End, queryParams, objQueryParams))
    {
        under = hitResultTrace.ImpactPoint;
        newUp = hitResultTrace.ImpactNormal;

        //Some math to get the new Axis
        FVector currentRightVect = GetActorRightVector();
        newForward = FVector::CrossProduct(currentRightVect, newUp);
        newRight = FVector::CrossProduct(newUp, newForward);

        //Build the new transform!
        FTransform newTransform(newForward, newRight, newUp, under);
        SetActorTransform(newTransform);
        retval = true;
    }
 
    return retval;
} 

The transitions are pretty jerky at this point. In the final version the idea is to play an animation that handles it. But I’m going to save that for later. The next post will feature adding in jumping, so the spider can jump up onto a wall, or down onto the floor, rotating correctly during the jump.

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *