- Home /
 
Editing camera bob script ?
Hello everyone, I know there are a lot of camera bob scripts but the ones I found are not suitable for what I want and I couldn't edit them so I need your help. I simply want my camera to bob upside & down slowly when it's in scope, which means when :
 InScope==true;
 
               . I have this script from http://www.unifycommunity.com , but it's about bobbing the camera when you walk, I don't want that, I want to put it into if boolean statement but I couldn't edit it, I need your help :).
 private var timer = 0.0; 
  var bobbingSpeed = 0.18; 
  var bobbingAmount = 0.2; 
  var midpoint = 2.0; 
  
  function Update () { 
     waveslice = 0.0; 
     horizontal = Input.GetAxis("Horizontal"); 
     vertical = Input.GetAxis("Vertical"); 
     if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0) { 
        timer = 0.0; 
     } 
     else { 
        waveslice = Mathf.Sin(timer); 
        timer = timer + bobbingSpeed; 
        if (timer > Mathf.PI * 2) { 
           timer = timer - (Mathf.PI * 2); 
        } 
     } 
     if (waveslice != 0) { 
        translateChange = waveslice * bobbingAmount; 
        totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical); 
        totalAxes = Mathf.Clamp (totalAxes, 0.0, 1.0); 
        translateChange = totalAxes * translateChange; 
        transform.localPosition.y = midpoint + translateChange; 
     } 
     else { 
        transform.localPosition.y = midpoint; 
     } 
  }
 
               Simply, I want that bob script to put into if statement like that :
if(InScope==true) { Bob(); }
please help, thanks :).
Answer by mpavlinsky · Jan 06, 2012 at 06:47 PM
Something like this?
 private var inScope = false;
 private var timer = 0.0; 
 var bobbingSpeed = 0.02; 
 var bobbingAmount = 0.2; 
 var midpoint = 1.0; 
 
 function Update () { 
     // Temporary input handling to turn the scope off and on.
     if (Input.GetKeyDown(KeyCode.Space)) {
         inScope = !inScope;
     }
     
     if (inScope) {
         var waveslice = Mathf.Sin(timer) - 0.5f; 
         timer = timer + bobbingSpeed; 
         if (timer > Mathf.PI * 2) { 
             timer = timer - (Mathf.PI * 2); 
         } 
         
         var newPosition = transform.localPosition;
         
         newPosition.y = midpoint + waveslice * bobbingAmount;
         transform.localPosition = newPosition;
     }
 }
 
              Your answer
 
             Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Idle camera bob script? 1 Answer
Headbobber script in C#? 3 Answers
Build for Windows : Camera rect always stretched 0 Answers
Transform Rotation - Maths Problem 2 Answers