- Home /
Changing Walkspeed for Run Button - Help!
Hi Im new to Unity3D and recently came from Game Maker to Unity3D for more game making options.
I am stuck on making a Run Button for my 2D platformer, I am using Javascript. I have some idea on how it all works, But I am still new to Unity so I need to get a custom to all its functions it has to offer. I have a run button to increase the walkspeed to a higher value by pressing a button say like left shift or something while moving left or right, although I am using my Xbox controller for this so it would be the whole "Input.GetButton("Fire1) and "Input.GetAxis("Horizontal")"
Although I am confused. Because I have rewritten this script out many times in many different spots and searching around I have been told to use it under the Function Update(). But it still does not work. I have tried making it its own function to no avail. Nor will it work under the normal movement (Even though the speed defiantly increased just it did it by default so it bought me to square one).
And I am absolutely stumped at this and would require some assistance as I have looked around all over the internet or to my knowledge I have and have found no solutions.
This is my code:
{
if (movement.enabled && Input.GetButton("Fire1")&& Input.GetAxis("Horizontal"))
movement.runSpeed = 10 * movement.walkSpeed; // < ---- RUNNING
}
I am however using this script with Javascript, I am using a character controller AND it is under the "function Update()" section. And Im sorry if I make some of you slap your heads in disbelief if this is simple or if the code isn't great. I just would like some guidance or at least a step in the right direction. Again, I am new.
Thank you for reading this and hope you help me out as soon as possible.
I think the { should be after if () so it would be like: if (movement.enabled && Input.GetButton("Fire1")&& Input.GetAxis("Horizontal")) { movement.runSpeed = 10 * movement.walkSpeed; // < ---- RUNNING }
The $$anonymous$$ovement code is this:
function Apply$$anonymous$$ovement() {
switch (controller.isGrounded) {
// The character is on the ground
case true:
movement.usedExtraJump = false;
if (movement.enabled) {
movement.offset = Vector3(Input.GetAxis("Horizontal"), 0, 0) * movement.walkSpeed;
if (Input.GetButtonDown("Jump")) ApplyJump(); // <-----------For JU$$anonymous$$PING
} else {
movement.offset = Vector3.zero;
}
break;
I tried your code, No errors but its just not doing anything.
Answer by toddisarockstar · Feb 20, 2015 at 05:45 AM
in function update, the computer is constantly scrolling and reading through your code. outside function update, the computer only reads it once and never again. outside function update is a good place to declair varables or whatever calculations / lookups that need to happen only once for use in update.
well im not familier with your controller setup. you can figure out the buttons yourself. but ill write a super basic code to surely make your object move at two speeds. this is a working code to get you thinking in the right direction.
var speed:float;
function Update(){
speed=.01;
if (Input.GetKey ("space")){speed=.05;}
if (Input.GetKey ("up")){transform.position.x=transform.position.x+speed;}
}
attach this to the object and your guy moves if you push the up arrow on the keyboard. push space and he moves at a different speed. you can change the buttons to whatever ya want and add the other directions. i just thought you seeing something work might help.
all my coding expirence is for unity. i am only familiar with javascript i am looking at your code wondering what the dots are for after your variables. all the unity.com references dont follow your format you are using. the simplest way to use varables is to declair them at the top of your script before function update. this way they show in the editor under your object so you can see whats happening.
for example your movement seems to be a true/false variable you want. to get you going put all your variables at the top like this.
var movement=false;
var speed:float;
var spot:Vector3;
function Update(){
if(movement==true){dosomething here;}
if(speed==10) {dosomething else;}
if(spot==whateverlocation){dosomethingelsecool;}
}
putting the dots is only for specifying funtions not variables in if statements. usually you only use = or == or < in if statements for variables for javascript in unity. of course good old && and || work too!
Will the var speed replace the walkspeed? Just want to ask if that needs to be changed or I can just used it? Also, I now get the error, "$$anonymous$$ Identifier speed"
that error means you mispelled something or you forgot to declair the variable at the top. anyways here is a full movement script. get rid of what you have and make a new javascript just like this. i tested it and it works.
var speed:float;
var upanddown:float;
var sidetoside:float;
function Update(){
speed=3;
upanddown=0;
sidetoside=0;
if (Input.Get$$anonymous$$ey ("space")){speed=8;}
if (Input.Get$$anonymous$$ey ("up")){upanddown=-.1;}
if (Input.Get$$anonymous$$ey ("down")){upanddown=.1;}
if (Input.Get$$anonymous$$ey ("left")){sidetoside=.1;}
if (Input.Get$$anonymous$$ey ("right")){sidetoside=-.1;}
upanddown=upanddown*speed*Time.deltaTime;
sidetoside=sidetoside*speed*Time.deltaTime;
transform.position=transform.position+Vector3(sidetoside,0,upanddown);
}
as far as "walkspeed" or "walk", its a variable and not a function so you can name variables anything you like. as long as you tell unity at the top what they are. thats what the "var" is for. it tells unity you are making a variable. functions start with capitol letters. variables do not.
put this script on the parent object you want to move and the arrow keys and spacebar will move any object this script is attached to. you can change the inputs to controller buttons if you like
I just like to say thanks, That helps, Now I just gotta get the Controller stick axis to work on this, Then add a jump and Im all good. Thanks!
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Animations Not Working 2 Answers
Stop walk sound when not grounded? 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Have an Int be in a Network Noob Networking Question 1 Answer