- Home /
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!
Need a little detail on what exactly its doing/doing wrong.
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.
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.
Put it inside the Update() method void Update() { if (Input.GetButtonDown("Fire1")) // Code here }
hope this help
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 ?
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;
}
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;
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?
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 :)
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 !
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;
}
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?