- Home /
joystick movement
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);
}
I don't know how to say it in english. but I mean when you don't touch your controller. is that better?
Ok, you have a joystick or equivalent, and your character moves even when the joystick is in the null position, is it correct? Your character moves very slowly in this case? Are you using Input.GetAxis to read the joystick?
If you talk about input keys(keyboard) such as w,a,s,d. Then you are using Input feature such as Input.Get$$anonymous$$eyDown. But if you release the button, it still moves, then it is about how it is programmed, it shouldnt generally moves according to the tutorials of Unity and traditional program$$anonymous$$g as soon as it is a rigidbody with force, you need to show some code
Answer by aldonaletto · Jun 14, 2011 at 11:07 AM
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.
I used the Input $$anonymous$$anager of unity and set the Dead to 0.1, but it doesn't work as expected.
Post the script where you read Input.GetAxis and move your character.
1- There's something wrong with the line below:
if(transform.localPosition.y + verticaal =bewegingsgrensrechts)
Is the = supposed to be >= or ?
2- You can avoid leakage values doing the following check:
if ($$anonymous$$athf.Abs(horizontaal)<0.01) horizontaal = 0;
if ($$anonymous$$athf.Abs(verticaal)<0.01) verticaal = 0;
3- The rigidbody will keep its velocity until friction consumes it; set the Drag rigidbody parameter to 0.5 or something alike to stop the object after a decent time.
solved, some of my good went missing with copy pasting. tested 2 and 3 but didn't fixed the problem.
Well, let's go back to the beggining:
1- When do you character start to move alone? As soon as the game starts? Or only after you move it with the joystick?
2- When the joystick is at null position, how does the character keep moving? Very slowly or at normal speed?
Answer by tnetennba · Jun 14, 2011 at 11:09 AM
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.
I used the Input $$anonymous$$anager of unity and set the Dead to 0.1, but it doesn't work as expected.