Post 19: Horsing Around

Time to give Bob a companion of the equine variety. Horses definitely fit into the whole feudal scheme of things. I started off by finding a free horse asset with a skeleton and placing it into the world. There was quite a bit that I did to make this good and I’m still missing some animations to finish it off, but I can get on those later. Functions implemented were:

  1. Prompt to mount
  2. Mounting character on top of the horse
  3. Dismounting off the horse
  4. Horse movement with walk, trot and sprint.
  5. Horse IK – making the hooves touch the ground
  6. Horse lead when turning
  7. Camera adjustment when player mounts horse.
  8. Character IK – making feet touch the ground and placing feet on stirrups when mounted.

Prompt to Mount

Firstly I needed a way for there to be an overlap between the horse and the player, I figured that seeing this is a horse specific task, I should attach a collision box here. Initially my next step was to use an ‘On Collider Overlap’ function that detected the player and utilised the UI widget that was created for assassinations and changed the text to “Press [E] to mount”. I then decided that I may wish to use [E] to interact with many other objects.

I then converted the OnCollision function into a more generic interact function that would access a blueprint interface, returning “Press [E] to” and then search the collided with actor for an action text, in the horses case “ride horse”. This is on a timer, which may change.

Mounting / Dismounting Horse

This played out very similarly to how I implemented weapon equipping in that I:

  1. Created a mount socket on the horse.
  2. Created the interact input [E]
  3. On this input, played a looping animation and attached the player to the mount socket.

This isn’t overly simplified, it was just a simple process as I’d done it before.

Dismounting was much the same, except a “Dismount” socket off to the side of the horse with a call to stop the riding animation.

Horse Movement

This was a tad tricky when I didn’t know about on-the-fly ‘Possess’ function that Unreal allows to be implemented. In a nutshell the horse movement implementation involved the following:

  1. Creating a horse movement blendspace with walk, run and gallop animations. I also threw in some turn animations which aren’t used at the moment (but I’ll get to that later).
  2. Using the Mount / Dismount input to change character possession over to the horse.
  3. Switching to a newly created horse-camera that will follow the mount socket around on a spring arm.
  4. Implementing the reverse when the player dismounts.
  5. Duplicating the movement function of the player (get vector forward and right).
  6. Putting in a turn lag, so the horse can’t do an instant 180 degree turn.

I did change up the movement a bit as initially the horse could move left and right and I felt like that just wasn’t correct. After removing the call to “Right Vector”, the horse is only able to move forward and backward. Unfortunately, this means that the turning left and right animations that I have in the blendspace are now redundant. I will need to have to figure out a way of detection new rotation and associating a left and right. That can come later down the track.

IK and Grounding

When using just a collider and gravity to run characters along the ground, there are a couple of issues that present themselves.

    1. The feet tend to always be too high or too low into the ground.
    2. The feet tend to maintain a horizontally flush line up, which becomes an issue on slopes and uneven ground

What we can do to fix this is manually set joints so that they touch ground directly below, whenever they are on a down cycle. I achieved this by:

    1. Creating a variable that would control the world location of each of feet (in this case, hooves) and linking it to a world location controller.
    2. Creating a tolerance bend level for the bottom two joints on each leg that would control how far each foot could be bent down.
    3. Creating a ray-cast from the bottom of the feet/hooves that would cast in a downward direction, returning the hit location.
    4. Using this information I would determine the world location of hit location and move the designated foot to that location, when within the tolerance range.

This took care of the feet, however there was some weird movement and an almost gyroscopic effect where the horse would maintain a constant height and the legs would almost bottom out over some areas. My fix for this was to take the half-height of the capsule and adjust it according to the ground below. In effect what this does is adjust the mesh inside the capsule so that the feet can always hit the ground, no matter how bumpy the terrain.

Finally I wanted to get the horse to rotate according to the slope. I achieved this by:

  1. Placing two ray-casts (one in the pelvis and one in the neck) pointing down, set to ignore self.
  2. I then took the hit locations of these and created a rotational plane that would rotate the mesh by it’s pelvis according to the Sine of the two hit locations.

The end results aren’t perfect (especially on a downward slope), but for now they work better than expected and will allow me to move on.

For the players feet in stirrups, I used the exact same method as the feet IK, except I mounted them to stirrup sockets.