For some reason, the Move command in my character controller script will not work. Here is my code:
function move () {
var controller : CharacterController = gameObject.GetComponent(CharacterController);
if (amIActive) {
var target : Transform;
if (Vector3.Distance (target.position, transform.position) <= distanceFromPlayer) {
if (walkAnimation != "") BroadcastMessage ("returnToZero", 0);
else inRangePlayer = true;
}
else {
inRangePlayer = false;
if (walkAnimation != "") BroadcastMessage (walkAnimation , 0);
transform.rotation.x = 0;
transform.rotation.z = 0;
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 10);
var move = transform.TransformDirection (0, 0, speed * Time.deltaTime);
controller.Move(move);
}
}
}
If I use something else instead of controller.Move, it works, but rigidbodies give me trouble. The enemy rotates towards the player, but does not move.
I’ve figured out what’s causing the problem. All the sub-meshes have colliders, and that is preventing my controller from moving. The reason why I have such colliders is when my character dies, he falls into pieces. (Robot of sorts.) Any suggestions?
Yes! Instantiate is your friend, and very useful in these situations, have a prefab of your mesh with the breakaway colliders, and then swap out when your character dies.