- Home /
CharacterController causing characters to fly around screen
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
As a work around... ins$$anonymous$$d of waiting 10 seconds, state if the velocity is greater than an exagerated known velocity. if your enemy can only move at say 10, say if(enemy.velocity>=20)//move to last waypoint.
I will test this without Char Cntl and post results if i find prob in code.
I noticed that if I take Time.deltaTime
out of my move equation, this doesn't happen. I thought using Time.deltaTime was supposed to make everything consistent regardless of fps...any ideas as to why this is happening?
You can store an array of the last, say, 5 Time.deltaTime
values and use the lowest value as your multiplier. This happens because when you hold the title bar the application pauses and this causes the value of Time.deltaTime
to get wildly big (I've tested this and it grows to it's max value of 0.3333333 ins$$anonymous$$d of more common values like ~0.015)
Answer by Zerot · Mar 25, 2012 at 02:38 PM
Change Time.deltaTime to Time.smoothDeltaTime. smoothDeltaTime is smoothed over multiple frames.
Time.deltaTime does make your game framerate independent, but not time independent. Time.deltaTime is nothing else then the time it took for the last frame to render. So at 60 fps it would be 1/60 = 0.016. At 10 fps the value would be 0.1. This value you then multiply with your normal movement, which makes it basically movement per second.
What happens, is that the deltaTime becomes a huge number because of the spike, which causes huge movement. For example, a 3 second spike causes a deltatTime of 3 seconds. Which means you move your character 3 seconds worth of time. Which is not what you want most of the time.
Another solution would be to have a max for your deltaTime with Mathf.Min(deltaTime, maxDeltaTime).
From what I've seen Time.deltaTime
doesn't ever go past 0.333333
Your answer
![](https://koobas.hobune.stream/wayback/20220613065458im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to transform velocity to world space before calling controller.move? 1 Answer
Removing velocity relative to the contact normal 1 Answer
Throwing knife doesn't throw correctly 0 Answers
How to make my Character run through walls with the use of the Character Controller component 1 Answer
Angular Friction 0 Answers