Bug with my Cam/Movement : Slowly offsetting more and more

I’m having a problem with my movement and camera scripts : When moving (especially sprinting)for a short time than stopping, the camera will center itself slightly ahead of where it should. Repeat this several times and it’ll be an entire screen ahead of where it should be.

In short : My camera continues to be offset slightly forward each time the object it’s following stops.

Using pastebin for code so page isn’t as cluttered with code.
CAMERA - Attached to the controlled unit, but for modding sake I give it the gameobject anyway.
http://pastebin.com/jbzVGB5P

MOVEMENT - Attached to controlled unit as well, but moves PlayerEntity
http://pastebin.com/0ZqUc8BQ

If I left out any details, or questions remain ask away. Criticism on scripts are also welcome.

Small shameful bump + edit.

It happens while the player is moving as well, much more evident when it’s moving fast.

The camera should be centering itself back on object. However it seems to be getting offset data.

Another bump. Pictures should help out a bit.
I’ve made 3 revisions to this script, but each one had the exact same problem.

#1 : After camera has moved to object - object has not moved yet. Notice alignment with console line.
http://img689.imageshack.us/img689/7554/21330160.jpg
#2 : Moved forward for several seconds. The camera has caught up to this already.
http://img801.imageshack.us/img801/2308/19576179.jpg
#3 : Continued moving forward - notice it is further back from #2.
http://img72.imageshack.us/img72/5580/42976503.jpg
#4 : Stopped. Compare with #1. It is obviously further back.
http://img121.imageshack.us/img121/9143/16702142.jpg

Do this for even a minute and the object will be completely offscreen. Move back for a minute and it’ll be back on screen. Move left and it’ll go off the right side of the screen.

Bump. Surely someone has ran into a problem like this…

I’m really not sure what the SmoothCamera function is supposed to be doing. Can you explain how it works?

Absolutely.

http://pastebin.com/khLitaHh

It averages the last 25 vector3s together, which than updates the MainCamera’s position to this.

wasd = movement
shift = running
While stationary align your mouse with the cube.
move forward for a short time. Come to a stop.
Notice cube is beginning to get behind the mouse.

I have checked to make sure CB_SmoothArray isn’t recording strange values or using ones it shouldn’t. When stationary, all of them are the same and it does not use any past 26.

361044–12522–$movementbughtml_139.rar (236 KB)

A week of debugging and giving up several times with this script has resulted the following : I am piss poor at math and horrid at debugging. I swore I did this several times.

Anyway, fixed. Solution :

function SmoothCamera(){ //xyz seperated from debugging
    //var x = CB_SmoothArray[0].x;
    //var y = CB_SmoothArray[0].y;
    //var z = CB_SmoothArray[0].z;
   [b] var x = CB_Entity.transform.position.x;
    var y = CB_Entity.transform.position.y;
    var z = CB_Entity.transform.position.z;[/b]
    CB_SmoothArray[CB_SmoothAmount]  = CB_Entity.transform.position; 
    for (i = 0 ; i < CB_SmoothAmount ; ++i){
        x += CB_SmoothArray[i].x;
        y += CB_SmoothArray[i].y;
        z += CB_SmoothArray[i].z;
        CB_SmoothArray[i] = CB_SmoothArray[i + 1];
    }
[b]     x /= CB_SmoothAmount + 1;
     y /= CB_SmoothAmount + 1;
     z /= CB_SmoothAmount + 1;[/b]
     //---- 
     
     x += CB_Offset.x;
     y += CB_Offset.y;
     z += CB_Offset.z;
     CB_Cam.transform.position = Vector3(x, y , z); //updates camera
     //CB_Cam.transform.position =CB_Entity.transform.position;
}

Ugh. At least I can move on now!