Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by crusherxman · Aug 22, 2013 at 06:49 PM · javascriptcharactercontrollercomponentspeedlength

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image crusherxman · Aug 24, 2013 at 01:00 AM 0
Share

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?

avatar image ExplodingCookie · Aug 24, 2013 at 02:17 AM 0
Share

Try commenting out the equivalent of line 20 in the original. That seems to be reassigning the value.

avatar image crusherxman · Aug 24, 2013 at 02:54 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges