How to make a Spider in UE4 Part 5

Here’s a video to show where the Spider currently is at.

As can be seen, there are some parts that are working well, such as the spider can walk on the walls, rotate on any surface, transition to other surfaces etc.

There’s a lot that isn’t working though. There are no collisions and the movement has a slide to it. It doesn’t correctly transition around outside edges.

I put the code, and project up on GitHub. github.com/eibonscroll/UE4SpiderAI

There was a problem with rotating the pawn on other surfaces. Basically, you can’t just set the rotation. Setting the transform does work.

void ASpiderPawn::RotateRight()
{
    FTransform TheTransform = this->GetTransform();

    FRotator rot = FRotator(0, rotationSpeed, 0);

    TheTransform.ConcatenateRotation(rot.Quaternion());
    TheTransform.NormalizeRotation();
    this->SetActorTransform(TheTransform);
}

This works on any surface angle correctly.

I use sockets on the Mesh to get the locations for the traces. The locations are updated each tick. Because there are a number of traces being done, can update locations of the sockets once per tick. This function creates a new transform. Previously I was trying to just set the rotation. Searching on the UE4 AnswerHub and the UE4 forums I went through all the posts I could find on wall walking, there were several people having trouble with strange angles when trying to rotate the Pawn onto different surfaces.

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

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

    FHitResult hitResultTrace;
    FCollisionQueryParams queryParams;
    FCollisionObjectQueryParams objQueryParams;

    queryParams.AddIgnoredActor(this);

    FVector under;

    FVector newUp;
    FVector newForward;
    FVector newRight;

    //Trace to get the surface normal in front of actor
    if (world->LineTraceSingle(hitResultTrace, BellySocketLoc, FrontSocketLoc, 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;
}

In the next post will work on getting the spider to walk around outside edges.

Share Button

Comments

How to make a Spider in UE4 Part 5 — 3 Comments

  1. Thank you for sharing the code!! I really need this for my study project!
    I try to implement the code on my project but it doesnt work 🙁
    the project on github doesnt work as well…it would be nice if you could make an video step by step !

    • Right click the UE4 project and select generate Visual Studio files. Then build the binaries from Visual Studio. You may also need to add the StarterCOntent to the project once you’ve opened it in UE4.

      It will not work if you just open UE4 and select the project, that’s not how it works. Refer to Epic’s site for UE4 documentation.

      I removed the binaries and the Visual Studio files from the repo because it seemed to imply the project didn’t need to be compiled to work, it does.

Leave a Reply

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