This is meant to make the main enemy of my game, a statue, follow you à laSlender.
It works fine, it follows well, only one problem: as soon as I start the game, the statue “throws itself to the ground” (rotates forward to be in a lying position on the ground) and runs around in the silly position. Any way to freeze it on the X axis to avoid this?
There’s a couple things I’ve experienced when trying to work with rotations that may or may not be the cause. First of all, rotations are hard to work with – lol.
So my two suggestions:
First maybe try adding in a condition check to make sure the direction you want this agent to face is non-zero. Mainly so that it always has a direction it CAN face. This may not have anything to do with it, but sometimes it does when working with rotations.
Here’s some example code I use when trying to rotate things like this:
Especially notice the “if ( _direction != Vector3.zero)” condition check.
Secondly, it sounds to me like the character is “hitting the deck” so-to-speak at the start of the game because it doesn’t have a target to rotate towards. If it is doing this, it means your rotation code is pointing it there. You have to somehow correct for this. Sometimes, I make an empty gameobject a child of the unit I’m wanting to face a certain directions, and use the empty game object as a reference point for the rotation.
Well, good luck. I find rotations difficult to work with myself. With enough time though, I seem to eventually work them all out.
Ok, read your question again, and now I’m thinking, maybe try adding more height to your targets “y” value position as well. If your target’s transform position is located towards its feet, or on the ground, then that is where your statue will look. So before inputing your target.position into the LookRotation function, do something like “target.position.y += 3” or something like that.