x


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

more ▼

asked Mar 24 '12 at 07:21 AM

junlee gravatar image

junlee
0 1 2 2

As a work around... instead 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.

Mar 24 '12 at 08:31 AM hijinxbassist

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?

Mar 24 '12 at 07:46 PM junlee

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 instead of more common values like ~0.015)

Mar 25 '12 at 09:56 AM asafsitner
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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).

more ▼

answered Mar 25 '12 at 02:38 PM

Zerot gravatar image

Zerot
370 2

From what I've seen Time.deltaTime doesn't ever go past 0.333333

Mar 25 '12 at 06:33 PM asafsitner
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1942
x982
x701
x330

asked: Mar 24 '12 at 07:21 AM

Seen: 754 times

Last Updated: Mar 25 '12 at 06:50 PM