- Home /
Enemy movement on uneven 3D terrain
When an Enemy in my game (RPGish) becomes aware of the player and gives chase, it falls through the terrain if there's a height difference (uneven/hilly terrain); Enemy has Character Controller/Motor but no Rigidbody. I was using this:
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
I have read that manipulation of the transform.position is common with object fall throughs. After many attempts, I ended up with this:
CharacterController cc = GetComponent<CharacterController>();
Vector3 forward = transform.TransformDirection(Vector3.forward);
cc.SimpleMove(forward * moveSpeed * Time.deltaTime);
and my Enemy has stopped falling through the ground. I changed moveSpeed from 4 (with failed/fall through code) to 299 to make it approx. the same speed and, to be honest, I have no idea why. Have I done something unexpected that will cause me headaches later with this movement code? Is there a standard practice for moving an AI unit around on uneven terrain?
Answer by Seth-Bergman · Jul 30, 2013 at 06:16 AM
I wouldn't worry about it... simplemove, as you can see, is in meters per second. Your other method uses the units (meters), but NOT by the second, by the frame. This (simplemove) is a perfectly valid way to handle things, I would say it is the standard.
Your answer