Controlling orientation of humanoids

I have non-player humanoids that follow targets around in my game:

They follow waypoints generated by pathfinding via Humanoid MoveTo. Unfortunately, orientation is completely automatic when using MoveTo. If I jump over the spider or sneak up behind it so it only has to move slightly to be in attack range, MoveTo will turn it a few degrees while walking backwards, but then it’s in range and stops moving, while it’s still facing the wrong way. I also don’t like how snappy rotation is when following waypoints, instead of smoothly turning. Even still, it’s not possible for the mob to back up and get distance between itself and the target, because then it’d be facing the opposite direction.

It looks like I need to manually control the orientation of the non-player humanoids. A BodyGyro might help me control the orientation, but it doesn’t handle flipping well, and may choose to turn 350 degrees instead of -10. BodyGyro is also a legacy physics object. Any suggestions for how I can control orientation for these mobs?

5 Likes

If you can manually detect when it needs to turn, you could try using an AlignOrientation object with the target attachment being on the target Character? Then when you’ve manually detected the npc is facing its target, you can either destroy or disable the AlignOrientation

I suppose this could give you some quirky effects if the player is above the npc, or jumping… enable PrimaryAxisOnly

1 Like

Wouldn’t that have the Spider align with the orientation of the Player?
i.e. if the Player faces the Spider, wouldn’t that cause the Spider to turn around to match the orientation?

Oh, I think I had a blank on how they work. I believe you are correct.

2 Likes

It sounds like you’ve identified enough ways you are unhappy with MoveTo do reasonably code your own replacement. If you do that, is there still a good reason for the spider to have a Humanoid? You don’t need it for to use Roblox animations, or to have health. It gets you the character shadow, which is arguably desirable. Issues with things turning the wrong way with BodyGyro or AlignOrientation are easily avoided: just don’t flip something 180 degrees in one update, spread it out over a few frames. Since you’re unhappy about discontinuous changes of orientation at the waypoints, you’ll want to smooth out the rotations with interpolation anyways!

If you need to instantly turn something, you can set the model CFrame and Gyro CFrame to the same desired value. As discontinuous jumps of any kind are fundamentally incompatible with Physics at spider scale, it might be wise to anchor, canCollide false, flip, possibly resolve a bad position by hand, and the re-able collisions and unanchor. With no precautions, you do risk the spider getting flung from the world slide-shot style.

5 Likes