Sign in to follow this  
Code Sage

Unity Player Input System

Recommended Posts

Code Sage    100

I am currently making a game in Java from scratch and I have a certain way of handling player input, but I don't feel like it is as efficient as it could be. Anyone feel like sharing how they handle Player Input or how they believe it should be handle in the most efficient matter possible. I realize there are probably a million ways to do it, but I'd just like to hear from the community and see what they have come up with.

Edited by Code Sage

Share this post

Link to post
Share on other sites
Finalspace    1147

I use a state based singleton class for keyboard, gamepad and mouse input. The states are polled at the start for every frame.

Important is that i record the previous state - to differentiate between key down (Continues) and key press (Single key).


In my player class i can then do something like that in its update method (Jump example):

if (Input.isKeyPressed(Input.KEY_UP)) {
	if (!jumpRequested && onGround) {
		// Jump key is pressed initially
		jumpRequested = true;
		jumpForce = maxJumpForce;
	} else {
		// Jump key is hold down
		jumpForce -= stepJumpForce;
		if (jumpForce <= 0f) {
			jumpForce = 0f;
} else {
	if (jumpRequested) {
		// Jump key is released
		jumpRequested = false;

// Apply force impulse when there is some jump force left
if (jumpRequested && jumpForce > 0f) {
	Vec2 f = new Vec2(0f, bodyMass * jumpForce);
	Vec2 c = body.getPosition();
	body.applyLinearImpulse(f, c, true);

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this