Stick an object to the ground while moving?

Hi everyone,

I’m trying to make a basic movement engine for a rpg game without using the character controller or a rigidbody. The reason I don’t want to use them is because there is no element of gravity in my game (you can’t jump or fall). I am trying to find the best way to get my player (a cube that can move in 8 directions) to stay attached to the ground, (which has sloped areas as well as flat) while moving (without using gravity). What would be the best way to go about this?

I have looked around the web for possible solutions and found that “raycasting” may have been used for this, but I have no idea how I would set that up.

Thanks in advance! (Sorry for all the parenthesis :I )

function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
var distanceToGround = hit.distance;
//use below code if your pivot point is in the middle
transform.position.y = hit.distance - transform.collider.bounds.extents;
//use below code if your pivot point is at the bottom
//transform.position.y = hit.distance;

Put this code in your objects movement script. It should work…