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 Borzi · Apr 22, 2013 at 03:49 PM · inputcharactercontrollerwalkcharactermotor

Button Input not working?

I added my own small script to the first person character controller "motor" script. It looks like this:

 //Player goes into "Walk" Mode
 var Walk : boolean = false;
 var maxWalkSpeed : float = 6.0;
 
 if ( Input.GetButtonDown ("Walk") )
 {
     maxForwardSpeed = maxWalkSpeed;
     maxBackwardsSpeed = maxWalkSpeed;
     maxSidewaysSpeed = maxWalkSpeed;
     Walk = true;
 }
 if ( Input.GetButtonUp ("Walk") )
 {
     maxForwardSpeed = maxForwardSpeed;
     maxBackwardsSpeed = maxBackwardsSpeed;
     maxSidewaysSpeed = maxSidewaysSpeed;
     Walk = false;
 }

Although I'm not getting any compile errors at all, the players speed does not reduce either. My input for "Walk" is currently that positive button is set to "left shift" and alt positive button is set to "right shift". Maybe I am using input incurrectly, maybe its that there is an error with the script, I'd be very grateful for any help, thank you in advance!

Comment
Add comment · Show 6
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 GC1983 · Apr 22, 2013 at 05:26 PM 0
Share

Need a little detail on what exactly its doing/doing wrong.

avatar image Borzi · Apr 23, 2013 at 07:01 AM 0
Share

Basically, Im trying to make the player go slower then at normal speed :P not sure if that helped what else do you need to know? Whenever I press shift ingame, the player does not slow down.

avatar image AlucardJay · Apr 23, 2013 at 07:28 AM 0
Share
  • added my own small script to the first person character controller "motor" script* : do you mean you edited the first person character controller "motor" script ? Or this is a separate script ?

The lines in the button up are not going to do anything :

 maxForwardSpeed = maxForwardSpeed;

If something equals itself, then .... no change.

I strongly suggest you look at this excellent answer by Aldo. It shows what you are trying to do. Tap into the character motor and modify the variables therein : http://answers.unity3d.com/questions/164638/how-to-make-the-fps-character-controller-run-and-c.html

And contrary to your title, if you watch the boolean Walk in the inspector while playing, you will see this toggling between true and false on button down and up.

avatar image GamingNewBie · Apr 23, 2013 at 07:37 AM 0
Share

Put it inside the Update() method void Update() { if (Input.GetButtonDown("Fire1")) // Code here }

hope this help

avatar image AlucardJay · Apr 23, 2013 at 07:43 AM 0
Share

Ga$$anonymous$$gNewBie : the OP is program$$anonymous$$g in uJS

 var Walk : boolean = false;

One would hope that this is within a function Update(){} , but the OP does need to include more information, hence my first line :

  • added my own small script to the first person character controller "motor" script* : do you mean you edited the first person character controller "motor" script ? Or this is a separate script ?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by AlucardJay · Apr 23, 2013 at 06:20 PM

From my comment : I strongly suggest you look at this excellent answer by Aldo. It shows what you are trying to do. Tap into the character motor and modify the variables therein : http://answers.unity3d.com/questions/164638/how-to-make-the-fps-character-controller-run-and-c.html

Firstly : you really should not modify the standard script. If you do, at least change the name of the script. Why? Well just imagine one day you accidentally re-import the first person character controller. Boom, in one step you have wiped out all your code, gone, overwritten, gone.

So my answer is a whole separate script (especially for future readers), all you have to do is attach it to the first person character controller.

I have changed the name of the boolean. You can use walk instead of isWalking, but just be aware by convention variable names are lowercase / camelCase. Read the commenting and make sure you understand what is happening.

 #pragma strict
 
 var walkSpeed : float = 7; // regular speed
 var runSpeed : float = 20; // run speed
 
 var isWalking : boolean = false; // boolean to show current speed state
 
 private var speed : float; // this variable stores the current speed
 private var chMotor : CharacterMotor; // store reference to Character Motor
 
 function Start() 
 {
     chMotor = GetComponent(CharacterMotor); // store reference to Character Motor
     speed = runSpeed; // start off at running speed
     isWalking = false; // set boolean to reflect speed is running speed
 }
 
 function Update() 
 {
     if ( Input.GetButtonDown( "Walk" ) )
     {
         speed = walkSpeed;
         isWalking = true;
     }
     
     if ( Input.GetButtonUp( "Walk" ) )
     {
         speed = runSpeed;
         isWalking = false;
     }
     
     // modify the character motor speed variables
     chMotor.movement.maxForwardSpeed = speed;
     chMotor.movement.maxBackwardsSpeed = speed;
     chMotor.movement.maxSidewaysSpeed = speed;
 }

Many thanks to Aldo, without his answer this script wouldn't be here because that's where I learned to do this =]


Edit : if you really really want to edit the standard script, first do as I suggested .... Rename it! , so no accidental imports overwrite all your work.

 #pragma strict
 
 var walkSpeed : float = 7; // regular speed
 var runSpeed : float = 20; // run speed
 
 var isWalking : boolean = false; // boolean to show current speed state
 
 private var speed : float; // this variable stores the current speed
 
 function Start() 
 {
     speed = runSpeed; // start off at running speed
     isWalking = false; // set boolean to reflect speed is running speed
 }
 
 function Update() 
 {
     if ( Input.GetButtonDown( "Walk" ) )
     {
         speed = walkSpeed;
         isWalking = true;
     }
     
     if ( Input.GetButtonUp( "Walk" ) )
     {
         speed = runSpeed;
         isWalking = false;
     }
     
     // modify the character motor speed variables
     maxForwardSpeed = speed;
     maxBackwardsSpeed = speed;
     maxSidewaysSpeed = speed;
 }
Comment
Add comment · Show 6 · 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 Borzi · Apr 23, 2013 at 08:25 PM 0
Share

Haha okay thanks :)

avatar image AlucardJay · Apr 23, 2013 at 08:33 PM 0
Share

No worries. Don't forget to mark as answered if this worked for you =]

P.S. I forgot the speed variable in the second script. It is fixed now.

 private var speed : float;
avatar image Borzi · Apr 23, 2013 at 09:00 PM 0
Share

Hahaha don't worry my friend I will as soon as I tried it, which I cant right now unfortunately :) I think I actually found an alternative way around this without manipulating the mainscript which, from what im hearing here, would be better?

avatar image Borzi · Apr 24, 2013 at 12:17 PM 0
Share

Okay so I found a great alternative here: http://answers.unity3d.com/questions/164638/how-to-make-the-fps-character-controller-run-and-c.html

Thanks for the help, gonna mark it as right as I presume its gonna work :)

avatar image AlucardJay · Apr 24, 2013 at 12:26 PM 0
Share

Yep, that would be the link i mentioned =]

  • strongly suggest you look at this excellent answer by Aldo. It shows what you are trying to do. Tap into the character motor and modify the variables therein* : http://answers.unity3d.com/questions/164638/how-to-make-the-fps-character-controller-run-and-c.html

Anyway, glad you found it and are learning lots of new things. Happy Coding !

Show more comments
avatar image
0

Answer by WheresMommy · Apr 23, 2013 at 09:36 AM

 //Player goes into "Walk" Mode
 var Walk : boolean = false;
 var maxWalkSpeed : float = 6.0;
 var speedChange : float = 0.5;
  
 if ( Input.GetButtonDown ("Walk") )
 {
     maxForwardSpeed = maxForwardSpeed * speedChange ;
     maxBackwardsSpeed = maxBackwardsSpeed  * speedChange ;
     maxSidewaysSpeed = maxSidewaysSpeed  * speedChange ;
     Walk = true;
 }
 if ( Input.GetButtonUp ("Walk") )
 {
     maxForwardSpeed = maxForwardSpeed;
     maxBackwardsSpeed = maxBackwardsSpeed;
     maxSidewaysSpeed = maxSidewaysSpeed;
     Walk = false;
 }
 
Comment
Add comment · Show 1 · 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 Borzi · Apr 23, 2013 at 05:51 PM 0
Share

First of all, thank you for your help, though unfortunately I have still encountered an error. The console gives me the error that 'maxForwardSpeed' and all other variables are "unknown identifiers". I already tried this before I posted:

 if ( Input.GetButtonDown ("Walk") )
 {
     maxForwardSpeed = maxForwardSpeed/2;
     maxBackwardsSpeed = maxBackwardsSpeed/2;
     maxSidewaysSpeed = maxSidewaysSpeed/2;
     Walk = true;
 }

Any ideas on whats wrong?

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

15 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

Related Questions

How to modify the base movement speed of the character in JS 1 Answer

How to move player with xbox controller 1 Answer

Moving player using character controller with new input system 1 Answer

Default Character Motor randomly jumping? 1 Answer

Mouse movement swings character around in circles 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