0 votes

I have a script where the mouse drags an item. However, I want that script to be able to round to the nearest 10.

For example;
10 by 10: Correct
260 by 570: Correct
10 by 15: Incorrect

Here is the script:

func dragItem():
    isDragging = true

    var mousePos = get_global_mouse_position()
    mousePos = mousePos.floor()
    self.set_position(mousePos)
    print(mousePos)

What can I do to make sure it always rounds to the nearest tenth?

in Engine by (162 points)

1 Answer

+3 votes
Best answer

GDScript has the handy stepify(float s, float step) function. For example:

var mousePos = get_global_mouse_position()
self.set_position(Vector2(stepify(mousePos.x, 10), stepify(mousePos.y, 10)))
by (1,554 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.