- Home /
Controller Joystick Sensitivity
Hi there, Im currently adapting my code so that when the player moves the joystick a little bit then the player will move slow but when they push the joystick all the way then they will go to max speed. However i dont really know how to do this.... i read it has something to do with "GetAxisRaw" however im all ready doing this. This is the code (c#) for the movement just now which when the player uses the joystick at any force they still move at max speed: //Force for x and y values float forceX = 0f; float forceY = 0f; //Vel equals absolute velocity float vel = Mathf.Abs(myBody.velocity.x); //H equals moving left/right float h = Input.GetAxisRaw("Horizontal"); //If h is greator than 0 if(h >0) { //if vel is less than maxvelocity if (vel < maxVelocity) { //If player is on the ground if (grounded) { GetComponent<AudioSource>().Play(); //moving along x axis is eqal to moveforce forceX = moveForce; print(vel); } else { //Gives more control in air, force is equal to move force times 1.1 forceX = moveForce * 1.1f; } } Vector3 scale = transform.localScale; scale.x = 1f; transform.localScale = scale; anim.SetBool("DetectiveRun", true); } else if(h<0) { if (vel < maxVelocity) { if (grounded) { GetComponent<AudioSource>().Play(); forceX = -moveForce; print(vel); } else { forceX = -moveForce * 1.1f; } } Vector3 scale = transform.localScale; scale.x = -1f; transform.localScale = scale; anim.SetBool("DetectiveRun", true); } else { anim.SetBool("DetectiveRun", false); }
``Any help would be great! Thanks!
Answer by Kishotta · Jun 12, 2017 at 03:28 PM
There are a couple of ways I can see you can eliminate some if statements. One way is to get a multiplier for your velocity that indicates direction using a ternary statement. Something like:
int direction = h > 0 ? 1 : -1; // direction = 1 for h > 0 and -1 for h < 0
Now to change direction you can simply do forceX = direction * moveForce
and transform.localScale = new Vector3 (1, direction, 1);
. No if statements required.
As for getting a "walk zone" on your input, typically you would set up two thresholds: a DeadzoneThreshold
and a WalkThreshold
. You might set these to 0.2 and 0.75 respectively (tweak them to get the feel you want).
These could be used to do something like this:
float hInput = Input.GetAxisRaw ("Horizontal"); // No smoothing on input
int direction = hInput > 0 ? 1 : -1; // Direction of input
hInput = Mathf.Abs (hInput); // We only care about magnitude now
if (hInput < DeadzoneThreshold) { // Input isn't significant enough to register
// Do nothing
} else if (hInput < WalkThreshold) { // Just enough to walk
if (grounded) {
forceX = direction * walkForce;
} else {
// Can't walk in air?
}
} else { // Enough to run
if (grounded) {
forceX = direction * runForce;
} else {
forceX = direction * runForce * 1.1f; // Air movement
}
}
It looks like you might want to read up on Finite State Machines and the State pattern if you plan on adding many more actions to your player, but you're on the right track.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Input not recognized after swapping control devices,Input being ignored (New Input System) 0 Answers
How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D) 0 Answers
Multiple Cars not working 1 Answer
C# Clamp joystick rotation 0 Answers