A forum mod kindly provided me the following code as an example of random walking behaviour:
var ground: Transform;
var gravity: Vector3;
var speed: float;
var motionDir: Vector3;
var ctrl: CharacterController;
function Start() {
ctrl = GetComponent(CharacterController);
NewDirection();
}
function Update () {
ctrl.Move((motionDir * speed + gravity) * Time.deltaTime);
}
function OnControllerColliderHit(collision: ControllerColliderHit) {
if (collision.transform != ground) {
NewDirection();
}
}
function NewDirection() {
motionDir = (new Vector3(Random.value - 0.5, 0.0, Random.value - 0.5)).normalized;
transform.rotation = Quaternion.LookRotation(motionDir);
}
I'm attaching it temporarily to my NPC GameObjects. Testing it, it works well; the NPCs run around and change direction nicely. However, I found that I needed to increase the project gravity, because those GameObjects weren't responding properly at default settings. So, at -1500 gravity, my NPCs fall around satisfactorily, but - I assume because gravity is used in this code - they're now hyperactive; changing direction a few times per second and basically just bobbling around, instead of 'walking'.
I (naively) thought I could just divide the gravity value within this code to return the behaviour to normal, but that didn't work. How can I retain both the necessary gravity value in my project AND the 'normal' behaviour of this code? Or, is there a way to achieve 'proper' gravity in the scene without cranking it up to such a ridiculous value?