Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm having a problem where, when I start my game, my game object (a 3D plane) immediately jumps 300 pixels or so. I suspect this has something to do with my joystick handling code that I've written to move this game object:

 xMin1 += Input.GetAxisRaw("LeftRight");
 yMin1 += Input.GetAxisRaw("UpDown");

 //_targetPlane is my game object
 _targetPlane.transform.position = new Vector3(xMin1, yMin1, 2.5f);

The object jumps even if I don't touch the joystick. Am I doing this wrong?

share|improve this question
1  
Do you run this in your update loop? When does the plane make the jump? When you move the joystick, or when you start the game? –  bummzack Apr 22 '13 at 14:52
    
Yes, this in my update method and it happens right when I start the game. Sorry, i should have made that clear. I'll edit now. –  N0xus Apr 22 '13 at 14:54
3  
If it happens right at the start, how is it related to moving your plane using a joypad? Also: Did you debug the values you get from Input.GetAxisRaw? Note that incrementing your values will increase your offsets really fast (with each tick), which is most likely not what you want. –  bummzack Apr 22 '13 at 14:56
    
Do yMin1 & xMin1 initialize with zero? –  golergka Apr 24 '13 at 11:48
    
Why did you roll back my edit, which was done to make the question fit the accepted style for the site? Please don't do that without good reason, questions that aren't titled as questions and don't specifically address the problem make the site less searchable and make it less likely that you will get answers. –  Josh Petrie Jan 6 at 17:34
add comment

1 Answer

You can confirm that the problem has anything to do with your joystick code by commenting out the code. If that fixes the behavior, the problem is likely how you are interpreting the data or initializing the variables. If it doesn't, you will need to debug your code elsewhere because there's nothing else we can do for you with this limited information.

If commenting out the code fixes the problem, take the following steps:

  • Verify that both xMin and yMin are initialized to zero using your debugger. It might be useful to log them or to log the position of the object every frame, that may help identify the problem.

  • If your joystick is not perfectly calibrated to rest at dead center (most aren't), you may be getting non-zero input even when the stick appears at rest (which will quickly accumulate and move your object). Log the values returns from GetAxisRaw to confirm this (Unity may or may not do the usual dead-zone computation to handle this for you, I'm not sure, but it's worth checking).

  • Note that GetAxisRaw doesn't do smoothing, and generally the values returned by GetAxisRaw should be weighted by delta time, as in the linked example code from GetAxisRaw. You may alternatively want to consider GetAxis instead.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.