Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Dryden Richardson · Jul 22, 2011 at 01:56 PM · javascriptcharactercontrollerspeed

How can I change the speed of a character controller on the fly?

Hi, I'm trying to intergrate a Sprint Fuction for the Untiy First Person Controller but I'm slightly baffled because of the way the code is written.

I want to press Left Shift to run faster and when i let go it goes back to normal speed and here's what I've been doing.

I've found the variable "maxForwardSpeed" and it is set as

 var maxForwardSpeed : float = 10.0;

And later on in the code I find...

 var zAxisEllipseMultiplier : float = (desiredMovementDirection.z > 0 ?
                                       movement.maxForwardSpeed : 
                                       movement.maxBackwardsSpeed) / 
                                       movement.maxSidewaysSpeed;

So if I'm doing it right could the code be like this?

 function Update()
 {
   if( Input.GetButtonDown( "Sprint" ) )
   {
   }
 }

But I'm stuck from there, I'm new to scripting and I have no Idea what to type into the Scripting Reference, can someone help me please?

Thanks for reading. (This is written in Javascript)

Comment
Add comment · Show 5
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 almo · Jul 22, 2011 at 02:07 PM 0
Share

Learn to use the 101010 button to format your code. It will help people answer your questions.

avatar image Dryden Richardson · Jul 22, 2011 at 02:20 PM 0
Share

Tried to use it, Just realised it didn't go to good, dang..

avatar image Dreamblur · Jul 22, 2011 at 05:29 PM 0
Share

Given the lack of info provided, just replace all references to speed in your script with:

 Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift) ? sprintSpeed : normalSpeed

That would be the general way of doing what you want. The more complex your script is, the more you have to modify that.

avatar image Dryden Richardson · Jul 23, 2011 at 01:15 PM 0
Share

:O Confused on the "? sprintSpeed : normalSpeed" part :P

avatar image coastwise · Jul 23, 2011 at 01:58 PM 0
Share

Thats an inline if statement (http://en.wikipedia.org/wiki/%3F: ).

 speed = Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift) ? sprintSpeed : normalSpeed;

means the same as

 if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift)) {
     speed = sprintSpeed;
 } else {
     speed = normalSpeed;
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by coastwise · Jul 22, 2011 at 07:26 PM

You could try the following:

 var sprintSpeed : float = 15.0;
 var normalSpeed : float = 10.0;

 var maxForwardSpeed : float = normalSpeed;

 function Update() {
     if (Input.GetButtonDown("Sprint")) {
         maxForwardSpeed = sprintSpeed;
     } else {
         maxForwardSpeed = normalSpeed;
     }
 }
Comment
Add comment · Show 5 · 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 Dryden Richardson · Jul 23, 2011 at 12:26 PM 0
Share

Okay thank you, I'll check it out and see if it works.

avatar image Dryden Richardson · Jul 23, 2011 at 01:14 PM 0
Share

In their own variables? So you mean.

var sprintSpeed : float = 15.0; or should it be.. var sprintSpeed = 15.0;

And

var normalSpeed : float = 10.0; or should be.. var normalSpeed = 10.0;

If so should I put them at the start of the script with the other Vars?

avatar image coastwise · Jul 23, 2011 at 01:16 PM 0
Share

At the start of the script with the other vars, then in the if statement you can maxForwardSpeed = sprintSpeed, etc. I edited the code in my answer to show you.

avatar image Dryden Richardson · Jul 23, 2011 at 01:33 PM 0
Share

And the character should run faster?

avatar image Dryden Richardson · Jul 23, 2011 at 04:02 PM 0
Share

Getting Error "expecting (, found 'Update'." Sorry for late Comment I've been busy

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I implement a SimpleMove max speed? 1 Answer

Increasing Camera "Bobbing" speed when character is sprinting 1 Answer

CharacterController problem 3 Answers

2D PlatformerController: Cumulative drift? 1 Answer

My CharacterController does not walk and face in the same direction 3 Answers


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