Well, I’m not particularly new, I’ve just been messing around.
I got the basic movements down on a third person character.
I decided to try to make him move faster, as in a run.
Here’s that portion of the code
if (Input.GetKey("g"))
{
speed += runSpeed;
}
else
{
speed = speed;
}
According to the .GetKey it should only run when the key is pressed, but for some reason it keeps moving in the “run speed” indefinately…
It doesnt seem like something should be wrong, but something is… Any help?
So, after you press “g” the character always moves faster, or it always does even before you press “g”? Here is some code that should work fine, just create a variable for walkSpeed, and just set runSpeed to how fast you want your character to move while pressing “g” and set walkSpeed to how fast you want your character to move when your not pressing “g”.
if (Input.GetKey("g"))
{
speed = runSpeed;
}
else
{
speed = walkSpeed;
}
instead you could have functions this way it wouldnt be called every frame, rather than per press heres how i would do it (may not be right
as im new too)
var speed = 5;
var runspeed = 10;
function Update () {
if(Input.GetKeyDown("g")) {
OnRun ();
print (" you are now running" );
}
if(Input.GetKeyUp("g")) {
print ("you are now walking");
OnWalk ();
}}
function OnRun () {
speed = 10; // or runspeed
}
function OnWalk () {
speed = 5; // or speed
}
it may not be the right way of doing it but it should work.
Thanks Astrauk, I did notice after I posted that it was adding up and up and up, and I didn’t think to reset it.
You sparked that thought, and thanks for the help.