I want a varible to... be a number when I press w

I’m not sure If I can explain this, But here it goes…

float lastx = transform.position.x;
float lasty = transform.position.y;

you see these variables? I like these variables, but they change all of the time as you can probably see. Is there a way I can sort of… pause the changing numbers when I press a certain key? I’m creating a jumping mechanic you see, and the way these variables are set up now sends my character to space. After you offer me a solution, can you also explain what I wanted so I can learn something? Thank you very much.

Have a look at this.

But you should start with some basic tutorials about scripting.

@Suddoha
I agree. I’ve been looking around and i haven’t had much luck. Thank you, I’ll see what this does. Oh no, I know How to (Input.getKey (keyCode.W)). My situation relies on pausing the variable when w is pressed. transform.position.x is a number that is constantly changing. I want that number to pause when I hold down w

The best solution is to stop adding the force that changes those values in the first place but if you just want a quick hack, you can try this:

Vector3 last;
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
last = transform.position;
}

while(Input.GetKey(KeyCode.W))
transform.position = last;
}

It’s a very ugly hack but it should do what you want…

1 Like

@MrDude

Funny enough, I was just looking at your last thread. Anyway, thank you very much. Even for a beginner, I should’ve known such problems would be solved with an if/while statement(s)

You’re gonna freeze the whole game with the second loop. :stuck_out_tongue:
Should be an if, not a while.

Actually, you are right… By bad!

I hate while loops :stuck_out_tongue: So easy to make a mistake and so hard to track it down…

Because you’ll never get out of the loop.
The input will be reset (getKeyDown/Up) refreshed (getKey) with every frame, but you don’t even have a chance to get to the end of the current because you’re stuck there.
Don’t forget that Update itself is a loop. so an if is what you should be using.

@MrDude
@Suddoha
one more quick problem I need the newly added “last” variable to be a float. I tried last.x but it gives the same result
“Use of possible unassigned feild ‘x’”

nevermind I fixed it myself