Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Rotation code breaks when using different resolution

Discussion in 'Scripting' started by HomeBearJosh, Feb 16, 2023.

  1. HomeBearJosh

    HomeBearJosh

    Joined:
    Dec 13, 2017
    Posts:
    32
    Issue
    My game requires rotating a handle around in a circular motion.
    Allow me to illustrate in glorious Paint.
    puzzle_illustration.png

    What I've got right now works fine in 1920x1080, but breaks in other resolutions, like 1280x720.
    Meaning, the handle jumps all over the place in unpredictable patterns, not following the cursor.
    Again, in 1080p, everything works perfectly.

    Code
    I can't seem to use 'insert code' in a way that doesn't give me a headache reading it, so here's screencap:
    code_rotation.jpg
    These are the methods responsible for rotating the handle.
    A 'virtual cursor' is used, not the hardware cursor (Input.CursorPosition), allowing me to manipulate the speed of its movement. The position setting for this virtual cursor is done as such:
    code_cursor.jpg

    The rotating handle is a simple GameObject with a Rigibody2D and 2D Collider Components.
    These live outside of the Canvas which I'm using for the obligatory 'exit this puzzle button'.
    The Canvas shouldn't be relevant, but I'll share the CanvasScaler settings just in case:
    code_canvas.jpg

    What I've Tried
    WorldToScreenPoint() works flawlessly in 1080p, but doesn't work at all in 720p.
    ScreenToWorldPoint() works okay-ish in both resolutions: at least the handle rotates, but there are still unpredictable 'jumps' and the handle doesn't follow the cursor as cleanly as when I use WorldToScreenPoint() in 1080p.
    I've tried all sorts of Canvas settings, but the dials and cursor don't really use the Canvas.

    If anyone could guide me in the right direction, I'd appreciate it.
    Is there an obvious flaw in the code?
    Am I messing something else that's obvious?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    GLORIOUS! I love it.

    Not sure what is going on for you but perhaps my "Rotate Z Via Drag" package can give you insight.

    It supports two ways of rotation, and I believe one of them is what you're looking for.

    It also handles any arbitrary resolution / aspect ratio.

    It does NOT handle changing it mid-drag. To do that you would need to:
    - detect the change
    - release the drag
    - reapply a fresh drag
    - continue

    Full package attached.
     

    Attached Files:

    HomeBearJosh likes this.
  3. HomeBearJosh

    HomeBearJosh

    Joined:
    Dec 13, 2017
    Posts:
    32
    Thank you very much - I'll give it a look!
     
  4. HomeBearJosh

    HomeBearJosh

    Joined:
    Dec 13, 2017
    Posts:
    32
    The script didn't help much, sadly.
    But I did find the solution!
    I noticed there was a flaw in the method that grabbed the start angle.
    I adjusted it as such and now everything works properly: Screenshot 2023-02-16 214923.jpg
     
    Kurt-Dekker likes this.