Hi all,
I am having a few issues with my characters becoming jerky, stuttering and being unable to move as the frame rate moves towards 60fps. I am emulating this by enabling VSYNC, although the same issues occur with VSYNC disabled is the frame rate drops.
A bit of Background: The game is a 2D top-down game using 2DToolkit. There is quite a bit of animation in the characters which is driven using Mecanim but I can’t see how this would affect things.
My character ‘follow’ code is basically a copy/paste of the included AIFollow class. (See below my version. All other functions within the script are the same as the standard AIFollow)
/** Update is called once per frame */
public void Update () {
if (path == null || pathIndex >= path.Length || pathIndex < 0 || !canMove) {
return;
}
//Change target to the next waypoint if the current one is close enough
Vector3 currentWaypoint = path[pathIndex];
currentWaypoint.z = tr.position.z;
while ((currentWaypoint - tr.position).sqrMagnitude < pickNextWaypointDistance*pickNextWaypointDistance) {
pathIndex++;
if (pathIndex >= path.Length) {
//Use a lower pickNextWaypointDistance for the last point. If it isn't that close, then decrement the pathIndex to the previous value and break the loop
if ((currentWaypoint - tr.position).sqrMagnitude < (pickNextWaypointDistance*targetReached)*(pickNextWaypointDistance*targetReached)) {
if (canMove)
ReachedEndOfPath ();
return;
} else {
pathIndex--;
//Break the loop, otherwise it will try to check for the last point in an infinite loop
break;
}
}
currentWaypoint = path[pathIndex];
currentWaypoint.z = tr.position.z;
}
Vector3 dir = currentWaypoint - tr.position;
// Rotate towards the target
tr.rotation = Quaternion.Slerp(tr.rotation, Quaternion.LookRotation(dir, Vector3.forward), _rotationSpeed * _mbpGame.GameTimer.DeltaTimeGame());
tr.eulerAngles = new Vector3(0, 0, tr.eulerAngles.z);
MinusRotatorGO.rotation = Quaternion.identity;
SymbolRotatorGO.rotation = Quaternion.identity;
Vector3 forwardDir = -transform.up;
forwardDir = forwardDir * _speed;
forwardDir *= Mathf.Clamp01(Vector3.Dot(dir.normalized, -tr.up));
forwardDir.z = 0;
tr.Translate(forwardDir * _mbpGame.GameTimer.DeltaTimeGame(), Space.World);
}
One point to note is the Rotation and Movement code: I am multiplying the values by _mbpGame.GameTimer.DeltaTimeGame(). This is just a wrapper around Time.deltaTime which multiplies deltaTime by a float, either 1f, 2f or 3f. This is so I can give the player the ability to speed up the game. I think this is where the problem may be originating as the issue is not there when it is purely multiplied by 1 (just deltaTime). deltaTime * 2f makes the issue worse and deltaTime * 3f makes the characters stutter and get totally stuck. With VSYNC disabled the fps is about 1000 on my machine and there are no issues at all. It’s just as the fps drops towards 60.
Any help in tracking down what could be causing this issue (i’m hoping it is a ‘school boy error’)!
Kindest regards in advance,
Matt.