- Home /
Camera Dampening
Heya all --
I'm trying to add dampening to a side-scrolling camera. Here is my code attached to the camera:
var smoothTime = 0.3; var cameraDampening = 10.0; var target:Transform; var newPositionX:float; var newPositionY:float;
function FixedUpdate () { newPositionX = Mathf.SmoothDamp(transform.position.x, target.position.x, cameraDampening, smoothTime); newPositionY = Mathf.SmoothDamp(transform.position.y, target.position.y, cameraDampening, smoothTime);
transform.position = Vector3(newPositionX, newPositionY, transform.position.z);
}
But what happens is that going left and right makes the camera go up and down, and vice versa. It seems like I've switched the axes, but I'm not seeing where. Any help?
Thanks so much!
$$anonymous$$ake another var named cameraDampeningY and use it for Y. The problem is that cameraDampening is used to "remember" the speed you are moving. when you reuse it for x, then y, it gets jerked around trying to move both ways. Each SmoothDamp needs its own var for the 3rd "speed" input. The solution below, Vector3.SmoothDamp, does this automatically (the third input is a Vector3, which is X's, y's and z's speed all in one.)
Answer by lucashaley · Mar 01, 2011 at 06:56 PM
I tried this instead, and it works pretty well:
function Update () {
// smoothing
transform.position = Vector3.SmoothDamp(transform.position, target.position, velocity, smoothTime);
// clamp z position for side-scrolling
transform.position = Vector3.Scale(transform.position, Vector3(1, 1, 0));
}