I’m making a RPG sort of game in Unity. Currently, I’m using the A* plugin from arongranberg and my character can move with the SimpleMove script that came with the it. But the problem is that the character doesn’t stay on the ground all the time. Its movement looks fine on a flat plane, but on a bumpy terrain, the character looks like it is floating in air.
I tried to attach Rigidbody component to the character, but it falls through the terrain.
Any suggestions on how I can get the character to look like its feet are stepping on the terrain?
Off the top of my head:
//in player class
//ray starts at player position and points down
Ray ray = new Ray(transform.position, Vector3.down);
//will store info of successful ray cast
//terrain should have mesh collider and be on custom terrain
//layer so we don't hit other objects with our raycast
LayerMask layer = 1 << LayerMask.NameToLayer("Terrain");
if(Physics.Raycast(ray, out hitInfo, layer))
//get where on the z axis our raycast hit the ground
float z = hitInfo.point.z;
//copy current position into temporary container
Vector3 pos = transform.position;
//change z to where on the z axis our raycast hit the ground
pos.z = z;
//override our position with the new adjusted position.
transform.position = pos;