- Home /
Camera position changes after shake
Hi! I have a script that shakes the camera when I walk over a triggered area. The problem I have is that the camera changes position after the shaking is done. Before the shaking, the Y-position for the camera is "0.9070835" and efter the shaking it's "0". I'm pretty newbie at coding so don't judge me :)
ShakeCam script:
var Cam: Transform;
static var Shake: float = 0;
static var ShakeAmount: float = 0.1;
static var BringDownTimer: float = 1;
function Update () {
if( Shake < 0){
Shake = 0;
}
if( Shake > 0){
Cam.transform.localPosition = Random.insideUnitCircle * ShakeAmount;
Shake -= Time.deltaTime * BringDownTimer;
}
else{
Shake = 0;
}
}
Answer by EHogger · Feb 06, 2013 at 02:21 PM
var Cam: Transform;
static var Shake: float = 0;
static var ShakeAmount: float = 0.1;
static var BringDownTimer: float = 1;
static var CamNeutralPosition: Vector3
function Start () {
CamNeutralPosition = Cam.transform.localPosition;
}
function Update () {
if( Shake <= 0){
Cam.transform.localPosition = Vector3.Lerp(Cam.transform.localPosition, CamNeutralPosition, Time.deltaTime);
}
if( Shake > 0){
Cam.transform.localPosition = Random.insideUnitCircle * ShakeAmount;
Shake -= Time.deltaTime * BringDownTimer;
}
}
This is one way. Hopefull it will give you the idea so that you can implement your own method. I've added a start function and a Vector3 that stores whatever your cameras starting position is. Then, this makes it so that if Shake is less than or equal to 0, the camera will smoothly move back to that starting position.
Wow thank you so much. It works perfectly :D It's kinda cool how it goes up slowly after the shake, it's like i'm standing up slowly. Thanks again!