I am using a CharacterController on AI controlled mobs in my game. The AI follows a simple waypoint system I made by targeting a waypoint and turning towards it and running forward using CharacterController.SimpleMove
Here is a snippet of my code that moves the AI:
var temp : Vector3 = Vector3(nextTarget.position.x, myTransform.position.y, nextTarget.position.z);
if((temp - myTransform.position) != Vector3.zero)
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation (temp - myTransform.position), rotationSpeed * Time.deltaTime);
//move towards target
controller.SimpleMove(myTransform.forward * curSpeed * Time.deltaTime);
This works fine, unless there is a lagspike, which causes all of the AI controlled characters in the game to wildly fly off screen. This can be reproduced 100% of the time by playtesting inside the unity editor, then holding left click on the top window bar of the unity editor (as if you were dragging the editor window around your screen, which causes the game to freeze) then letting go.
My game is being run through the web player, and it also happens there whenever any sort of FPS drop occurs from outside lag sources (such as having the game open in the browser, then clicking the play button inside unity to playtest there at the same time).
I tried to workaround this by resetting the position of the characters to their last visited waypoint if they don’t reach their destination within 10 seconds, however their velocity from being thrown around stays with them and they continue to fly around after resetting position.
Is there something I’m doing wrong in my movement script? Is there a way to fix this?
Thanks