I use a joystick to move around.
When I don’t touch my joystick, my character still moves.
Is there a way to stop this?
private var verticaal : float=0;
private var horizontaal : float=0;
private var bewegingsgrenslinks :float;
private var bewegingsgrensrechts :float;
var script;
script = this.gameObject.GetComponent(speler);
bewegingsgrenslinks = script.bewegingsgrenslinks;
bewegingsgrensrechts = script.bewegingsgrensrechts;
function Update(){
horizontaal = Input.GetAxis(script.horizontaal);
verticaal = Input.GetAxis(script.verticaal);
if(transform.localPosition.y + verticaal <=-1)
{
verticaal = 0;
}
if( transform.localPosition.x + horizontaal <= bewegingsgrenslinks ||transform.localPosition.x +horizontaal >=bewegingsgrensrechts)
{
horizontaal = 0;
}
rigidbody.velocity = Vector3(horizontaal,verticaal,0);
}
Input.GetAxis can return small values when the joystick (or other game controller) is slightly uncalibrated. Due to this, you should never compare Input.GetAxis to zero, like this:
if (Input.GetAxis("Vertical")>0){
// moves the character forward with velocity maxSpeed
Since Input.GetAxis returns a float value between -1 and 1, the best approach is to use something like this:
var dz = Input.GetAxis("Vertical");
if (Mathf.Abs(dz)>0.01){
// move the character forward with velocity dz * maxSpeed
The value returned by GetAxis is compared to a “safety margin”, so small values will not move the character. If this doesn’t solve your problem, please post the script where you read the game controller.
It is likely that you are not setting a dead zone on your controller.
What you need to do is set a small area on the stick that does not respond to any input.
To do this when you do Input.GetAxis you need to do something like:
float ax = Input.GetAxis("Mouse X");
if( ax < 0.1f && ax > -0.1f)
{
ax = 0.0f;
}
You now have a dead zone between -0.1 and 0.1 on that particular axis.
This issue is caused due to controller stick not sitting exactly at 0.0 even when noone is using them.