x


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);   
}
more ▼

asked Jun 14 '11 at 09:28 AM

Aldwoni gravatar image

Aldwoni
865 27 30 34

What does it mean: put my controller down?

Jun 14 '11 at 09:58 AM aldonaletto

I don't know how to say it in english. but I mean when you don't touch your controller. is that better?

Jun 14 '11 at 10:00 AM Aldwoni

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?

Jun 14 '11 at 10:07 AM aldonaletto

If you talk about input keys(keyboard) such as w,a,s,d. Then you are using Input feature such as Input.GetKeyDown. 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 programming as soon as it is a rigidbody with force, you need to show some code

Jun 14 '11 at 10:10 AM byerdelen

I use Input.GetAxis on my joystick

Jun 14 '11 at 10:13 AM Aldwoni
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Jun 14 '11 at 11:07 AM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

I used the Input Manager of unity and set the Dead to 0.1, but it doesn't work as expected.

Jun 14 '11 at 12:13 PM Aldwoni

Post the script where you read Input.GetAxis and move your character.

Jun 14 '11 at 12:34 PM aldonaletto

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 (Mathf.Abs(horizontaal)<0.01) horizontaal = 0;
if (Mathf.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.

Jun 14 '11 at 02:01 PM aldonaletto
  1. solved, some of my good went missing with copy pasting. tested 2 and 3 but didn't fixed the problem.
Jun 14 '11 at 02:17 PM Aldwoni

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?

Jun 14 '11 at 05:09 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jun 14 '11 at 11:09 AM

RoflHarris gravatar image

RoflHarris
971 5 8 13

I used the Input Manager of unity and set the Dead to 0.1, but it doesn't work as expected.

Jun 14 '11 at 12:13 PM Aldwoni
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x955
x215
x35

asked: Jun 14 '11 at 09:28 AM

Seen: 3169 times

Last Updated: Jun 28 '11 at 07:36 AM