- Home /
Getting a character to stay on terrain
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?
Take a look at this to start you off on your epic journey:
Wow... So the character controller doesn't have this "epic" thing with it? And I have to do it on my own?
In the description of the video (I should have included it sorry) you will find a link to the unity projects page. If you click the link for mecanim (http://u3d.as/content/unity-technologies/mecanim-example-scenes/3Bs) you will find the project files.
So for something like having an object to stay on the terrain's slope is not possible, or at least not easy, in unity 3.5.x?
Thats from what I can see. Or from what unity can do automaticly. Try downloading it and downgrading it, there is a possibiltiy it might be made for unity 3.5.x and updraded in unity 4.
Answer by Khada · Mar 07, 2013 at 03:18 PM
Off the top of my head:
//in player class
void Update()
{
//ray starts at player position and points down
Ray ray = new Ray(transform.position, Vector3.down);
//will store info of successful ray cast
RaycastHit hitInfo;
//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");
//cast ray
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;
}
}
Your answer