Help needed: As you decrease the window size, the character is jumping higher.

Hello,
How is everyone?

I need your help for a 2D game.
Problem: As you decrease window size, the character is jumping higher. And also, moving faster.

Details:

  • I am using a parent with a Canvas Scaler (Scale with Screen Size & Expand) to keep the game at 16:9.
  • I am using the new Input System with very basic movement code. ( obj.movement = context.ReadValue().x; ).
  • I have tried changing Update to Fixed Update.
  • Velocity value seems normal.

Question:
Could you please try to point me to something to check that could be useful? Any thoughts would likely be extremely helpful because I am out of ideas.

Thank you for reading my question :roll_eyes:

Sounds like either your speed of jump calculation is wrong, or more likely you’re using CanvasScaler mode that maps a varying scale to the screen. You probably want to use Match Width Or Height and set it to full Height match.

1 Like

Thank you for your reply, Kurt - it is very helpful and I really appreciate it. :slight_smile:

The issue seems to be - the character is being scaled down, while his box collider remains the same size.

The character being scaled down is working as intended because I want to force the camera to render the exact same horizontal distance for any screen size. So the less screen width - the smaller the character is.

Imagine you have that issue - is there a way you would scale down the box collider to the size of the character? Or what other solution would you attempt yourself?

I have found OnRectTransformDimensionsChange which triggers excellently when the screen size changes. The question is how do I find the character’s new size to set the box collider size?

This shouldn’t be happening unless something is doing it in code.

I’ll first speculate that this isn’t actually happening, unless you have seen actual numerics of the collider change.

Rebuild a much-simpler proxy of the character and hook it up. Same issue?

Beyond that, start printing out values used to compute height of jump. There will be a discrepancy somewhere.

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.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

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

When in doubt, print it out!™

1 Like

Thank you Kurt, you are very helpful! :roll_eyes:

The good news is, I have successfully replicated the issue in a new, simple project. The bad news is - I still do not know what is causing the issue. Here are the full details of the simple project:

Problem: Character does a higher jump the more narrow the window is:
8419701--1113870--upload_2022-9-6_19-41-36.png

The only code in the project:

    public Rigidbody2D character;
    public float jumpforce;


    void Update()
    {
        if(Input.GetKeyDown("space"))
        {
            character.velocity = new Vector2(character.velocity.x, jumpforce);
        }
    }
void Start()
    {
        Screen.fullScreen = false;

    }

Objects:
8419701--1113852--upload_2022-9-6_19-26-43.png

Canvas:
8419701--1113861--upload_2022-9-6_19-28-27.png

Camera:
8419701--1113855--upload_2022-9-6_19-27-48.png
Character:
8419701--1113864--upload_2022-9-6_19-29-5.png
Additional tests showed that:

  • Velocity during the issue remains unchanged.
  • The issue is only present in the stand-alone version.

If anyone has any potential clues on what the source of this is, please feel welcome to share it. Thank you so much :eyes:

Update & Solution:

I have fixed the issue by switching to a Physical Camera.

1 Like