- Home /
How to Logically Match Ground Slope While Using This Code?
For the game I'm working on, I'm using a small figurine on a circular base to represent the character, and I'm using this code to rotate the figure to face the direction of movement, but I also need help getting it to logically match the slope of the ground below it.
if (moveH == 0 && moveV == 0) {
rotDir = new Vector3(moveH, 0.0f, moveV);
}
else if (moveH != 0 || moveV != 0) {
rotDir = new Vector3(moveH, 0.0f, moveV);
finDir = rotDir;
}
rotDir = new Vector3(moveH, 0.0f, moveV);
transform.rotation = Quaternion.LookRotation(finDir);
Think of it like sliding a pawn down a slope and on to the top of a table, how it would maintain a proper angle away from the ground beneath it even when partially on the slope and partially on the table. What would I need to do to this code to also have it do that? I've thought about using four points around the figure and having raycasts for each but I'm not quite sure exactly what I would do to get the results I need with that.
Answer by Kishotta · Mar 18, 2018 at 07:09 AM
You can pass a second Vector3 to Quaternion.LookRotation
to specify the "up" direction. If you already have a raycast for detecting the ground, you can just use Quaternion.LookRotation(finDir, hit.normal)
Thank you, I didn't realize you could add a second Vector3 to that.