- Home /
Player Sprint button not working properly...
Hi guys i am making a rpg game in unity for mobile...i had created a run button (SPRINT) and joystick ...joystick was working perfectly and i added run button with event trigger it also worked perfectly but when i play the game the player move slightly in one direction with little speed ( i saw it on inspector and i was not even pressing run button )....this is my script.....
private Rigidbody Player;
private bool IsRunning = false;
public float RunMoveForce = 20f;
private bool _RunPressed = false;
public float moveForce = 10f;
private FixedJoystick joystick;
private Animator anim;
void Awake () {
Player = GetComponent<Rigidbody>();
joystick = GameObject.FindWithTag("Joystick").GetComponent<FixedJoystick>();
anim = GetComponent<Animator> ();
}
void Update () {
//JOYSTICK CONTROLS.....
Player.velocity = new Vector3(joystick.Horizontal * moveForce, Player.velocity.y, joystick.Vertical * moveForce);
if(joystick.Horizontal != 0f || joystick.Vertical != 0f) {
anim.SetBool("Walk" , true);
transform.rotation = Quaternion.LookRotation(Player.velocity);
} else {
anim.SetBool("Walk" , false);
}
//RUN BUTTON ...
if (_RunPressed) {
Player.velocity += transform.forward * RunMoveForce;
transform.rotation = Quaternion.LookRotation (Player.velocity);
anim.SetBool ("Run 0", true);
IsRunning = true;
}
else if (!_RunPressed) {
Player.velocity += transform.forward * Time.deltaTime * moveForce;
anim.SetBool ("Run 0" , false);
IsRunning = false;
}
}
public void onPointerDownRun() {
_RunPressed = true;
}
public void onPointerUpRun() {
_RunPressed = false;
}
Answer by Bavr · Aug 08, 2019 at 12:52 PM
Are you using gravity in your rigidbody? There might be some velocity in y direction because of gravity and that's why it's moving slightly.
I am not using gravity... but it still moving in x axis
The joystick.Horizontal is 100% == 0 while you are doing nothing? If you don't have movement in y, I would disable it.
please can you explain that to me ...i ill apperciate that....
Answer by Dawdlebird · Aug 09, 2019 at 02:50 PM
You mean to say the player is moving without you touching controls? This might be a deadzone issue; controllers sometimes signal tiny bits of input even though you're not pressing anything. It's a hardware thing, and can get worse with older controls (worn down sensors, dust buildup, etc). For this purpose it is common to use a "deadzone" for controls (you can see values for it under project settings: input named "dead". The higher that value, the less sensitive your controller will be). My xbox controller fell quite a few times and now has massive false input, so I set this deadzone pretty high to prevent my characters to start walking to the right on their own. You can do this in code to by putting in a condition like "if (Mathf.abs(joystick.Horizontal > 0.15f)" and only then run your movement code.
I dont think so because when i move the player then it starts in x axis and z axis maybe its the issue in the code can you find my mistake ...apperciate your answer.
Your answer
Follow this Question
Related Questions
Simulating Fighter Jet Physics (with addForce () ) 2 Answers
Problem stopping rigid body after adding a force 0 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Multiple Cars not working 1 Answer
Why doesn't my rigidbody velocity limiter script work properly? 1 Answer