Unity Fixed Joystick issues with new game

Hello,

So i am making a 2d top down game, and so far the joystick with the top down works, for the first game, when the player selects the character, then goes into the “World” level which is another scene, then i have a menu or pause button, when the player clicks or taps it, has the option to Exit or quit the game, which takes them to the Main select screen… BUT the problem is, after that, when the player tries to play the game again, no matter which options, i realize that the joystick in the game magntitude:

joyStick.Direction.magnitude

doesnt get picked up or anything no matter how much you move it… i checked by using the debug.Log… it never changes it from 0, therefore the character wont move… But again, it worked when the player first plays the game… but after the player exits the game, and goes into a new game… the joystick/joyStick.Direction.magnitude doesnt work :frowning: any idea?

sorry also, when the player clicks QUIT button game, here is the code below, which seems simple:

    public void ConfirmQuitPressed()
    {
        SceneManager.LoadScene(0);
    }

but when the player goes back into the game… the joystick joyStick.Direction.magnitude doesnt seem to work, again I added a debug.log to the screen, and when moving the joystick… always does 0… anything? have to reset?

k also update, i did a debug.log on the Joystick.Horizontal and Joystick.Vertical which I turn it into a vector 2 variable:

input = new Vector2(joyStick.Horizontal, joyStick.Vertical).normalized;

and I do see Horizontal/Vertical input decimal/float which i get the input and see it on the debug.log console… but not sure why the other one, which is the joyStick.Direction.magnitude doesnt work??

Ok so digging even further, it seems its the FixedUpdate() issue, which here is the code:

    private void FixedUpdate()
    {
        //Debug.Log(joyStick.Direction.magnitude);

        if (joyStick.Direction.magnitude >= 0.33f) //Fast
        {
            //Move character FAST
            animalrb2d.MovePosition(animalrb2d.position + velocity * fastSpeed * Time.fixedDeltaTime);
        }
        else if (joyStick.Direction.magnitude >= 0.01f) //Slow
        {
            //Move character SLOW
            animalrb2d.MovePosition(animalrb2d.position + velocity * slowSpeed * Time.fixedDeltaTime);
        }
        else
        {
            animalrb2d.MovePosition(animalrb2d.position + velocity * Time.fixedDeltaTime);
        }

    }

I did put a Debug.Log() for joyStick.Direction.magnitude in the Update, and i see it, however when I put it in the FixedUpdate method, it doesnt show, but the above code… now i am trying to figure out maybe i need to reset the direction? even though its already 0? or maybe not? thoughts?

The issue is you’re using FixedUpdate to poll inputs. Inputs should only ever be polled up Update. FixedUpdate is for physics related matters.

The the value of your joystick to a local field using Update, and act on those values in FixedUpdate.

gotya thank you for that spiney199, i did move the input to update from fixed update, i tried it out, still same problem, when i go into a new scene, or should say… go from quitting game, which is the game level 1 scene, to menu scene, then back to level scene 1… thats when the joystick still doesnt work… any ideas?

First and foremost, find out what’s going on.

A joystick provides an input data stream over time. That’s it.

Print that out to the Debug.Log() or else onscreen.

Get that nailed down 100% first.

Don’t even involve ANY other code until the joystick is producing rock solid input values.

After you have confidence the input is being accepted properly, then study how the code “downstream” of that, eg your player movement logic, processes the input.

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

ugh I am one big idiot, so… when I pause to quit the game, or change scene, the Time.timescale is still set to one, so when i go back to the scene, its still set to one, I checked this by using Debug.Log(Time.timescale)… sorry everyone…

and thank you for that as well kurt-dekker :slight_smile:

hope this helps, Debug.Log() is a great way to troubleshoot :wink:

thanks again for your input spiney199, about the fixed update and update

lol sprinkling Debug.Log… lol love that phrase and yes, i will start doing that more often to make sure its stepping into the function/method and through the steps… thanks again Kurt-Dekker :wink:

1 Like