- Home /
Increasing Camera "Bobbing" speed when character is sprinting
I'm having a hard time trying to increase my camera bobbing speed when I'm sprinting (while pressing the Left Shift key) I've edited the script and I came up with no errors witch is good but my bobbing speed doesn't change.
Here's my scripts:
Camera Bobbing Script:
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, 1.0, 1.0);
translateChange = totalAxes * translateChange;
transform.localPosition.y = midpoint + translateChange;
}
else {
transform.localPosition.y = midpoint;
}
}
Character Controller Script:
var walkSpeed: float = 7; // regular speed
var crchSpeed: float = 3; // crouching speed
var runSpeed: float = 20; // run speed
var footStepLengthSprint : FootSteps; //footstep length when running
var headBobbingSpeedSprint : HeadBobbing; //headbobbing length when sprinting
private var chMotor: CharacterMotor;
private var ch: CharacterController;
private var tr: Transform;
private var height: float; // initial height
function Start(){
chMotor = GetComponent(CharacterMotor);
tr = transform;
ch = GetComponent(CharacterController);
height = ch.height;
}
footStepLengthSprint = gameObject.GetComponent(FootSteps);
headBobbingSpeedSprint = gameObject.GetComponent(HeadBobbing);
function Update(){
var h = height;
var speed = walkSpeed;
if (ch.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift")){
speed = runSpeed;
footStepLengthSprint.audioStepLength = 0.4;
}else{
footStepLengthSprint.audioStepLength = 0.65;
}
if (ch.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift")){
speed = runSpeed;
headBobbingSpeedSprint.bobbingSpeed = 0.35;
}else{
headBobbingSpeedSprint.bobbingSpeed = 0.6;
}
if (Input.GetKey("left ctrl")){ // press C to crouch
h = 0.5 * height;
speed = crchSpeed; // slow down when crouching
}
chMotor.movement.maxForwardSpeed = speed; // set max speed
var lastHeight = ch.height; // crouch/stand up smoothly
ch.height = Mathf.Lerp(ch.height, h, 5*Time.deltaTime);
tr.position.y += (ch.height-lastHeight)/2; // fix vertical position
}
As many of you might notice, yes, these are edited by me when I was trying to add this feature... witch didn't work well at all :/
If you guys have a good way to solve my question, post your answers below! I would appreciate you help :)
Thanks!
Answer by ExplodingCookie · Aug 23, 2013 at 08:12 PM
Try to merge lines 29 - 41 and add a debug statement to see if the value is getting set.
if (ch.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift")){
speed = runSpeed;
footStepLengthSprint.audioStepLength = 0.4;
headBobbingSpeedSprint.bobbingSpeed = 0.35;
//This logs the bob speed to the console in the bottom left corner of the editor
Debug.Log("The Sprint Bobbing is " + headBobbingSpeedSprint.bobbingSpeed.ToString());
}else{
footStepLengthSprint.audioStepLength = 0.65;
headBobbingSpeedSprint.bobbingSpeed = 0.6;
}
If your debug output still reads the normal speed, then you should check through all the scripts with access to the bobbing controller to see if any of them set the bob value.
Well I haven't got any errors with this and I've dragged my Camera with the Bobbing Script but every time I enter my scene, my attachment disappears! I've tried to attach my Camera while I was testing my game in the editor but my bobbing speed is stuck at 0.6
even when I'm walking. Do I need to add a "Get.Compoment" function in order to make it work?
Try commenting out the equivalent of line 20 in the original. That seems to be reassigning the value.
Ok, I've made my changes but now my Character isn't sprinting at all when I press the Left Shift $$anonymous$$ey...
Your answer
Follow this Question
Related Questions
How can I implement a SimpleMove max speed? 1 Answer
How can I change the speed of a character controller on the fly? 1 Answer
Unslide does not revert back to normal 1 Answer
How to disable/enable SSAOEffect or other specific Components? 1 Answer
Why is C# joystick taking 300 times slower to execute then javascript Joystick? 1 Answer