Skip to main content
Cleanup
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

How to use tweens to spin clocwisea wheel clockwise in Godot Engine?

Hello howHow can I spin a roulette wheel in a clockwise direction using a tween? 

I've successfully managed to rotate counter-clockwise to the exact segment, but clockwisechanging the direction to go clockwise is still a problem.

roulette wheel

extends Control


const DEGREES := 360.0
@onready var wheel := $Front as TextureRect

enum Directions {CCLOCKWISE=-1, CLOCKWISE=1}
@export var direction := Directions.CLOCKWISE
@export var segment_count := 8
@export var spin_time := 2.0
@export var spin_count := 1
var spinning := false


func spin(to_segment: int) -> void:
    if spinning:
        return
    spinning = true
    print(to_segment)
    randomize()
    var d := DEGREES / segment_count
    var r := d / 2
    var t := (to_segment-1) * d - r
    t += fposmod(wheel.rotation_degrees, DEGREES)
    #t += randf_range(0, d)  # add slight offset
    var target := wheel.rotation_degrees - (spin_count * DEGREES) - t

    var tween := create_tween().set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_OUT)
    await tween.tween_property(wheel, "rotation_degrees", target, spin_time).finished
    spinning = false


func _input(event: InputEvent) -> void:
    if event is InputEventMouseButton:
        if event.pressed:
            spin(randi() % segment_count + 1)  # pick random segment

How spin clocwise wheel in Godot Engine?

Hello how spin roulette wheel in clockwise direction using tween? I've successfully managed to rotate counter-clockwise to exact segment, but clockwise direction is problem.

roulette wheel

extends Control


const DEGREES := 360.0
@onready var wheel := $Front as TextureRect

enum Directions {CCLOCKWISE=-1, CLOCKWISE=1}
@export var direction := Directions.CLOCKWISE
@export var segment_count := 8
@export var spin_time := 2.0
@export var spin_count := 1
var spinning := false


func spin(to_segment: int) -> void:
    if spinning:
        return
    spinning = true
    print(to_segment)
    randomize()
    var d := DEGREES / segment_count
    var r := d / 2
    var t := (to_segment-1) * d - r
    t += fposmod(wheel.rotation_degrees, DEGREES)
    #t += randf_range(0, d)  # add slight offset
    var target := wheel.rotation_degrees - (spin_count * DEGREES) - t

    var tween := create_tween().set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_OUT)
    await tween.tween_property(wheel, "rotation_degrees", target, spin_time).finished
    spinning = false


func _input(event: InputEvent) -> void:
    if event is InputEventMouseButton:
        if event.pressed:
            spin(randi() % segment_count + 1)  # pick random segment

How to use tweens to spin a wheel clockwise in Godot Engine?

How can I spin a roulette wheel in a clockwise direction using a tween? 

I've successfully managed to rotate counter-clockwise to the exact segment, but changing the direction to go clockwise is still a problem.

roulette wheel

extends Control


const DEGREES := 360.0
@onready var wheel := $Front as TextureRect

enum Directions {CCLOCKWISE=-1, CLOCKWISE=1}
@export var direction := Directions.CLOCKWISE
@export var segment_count := 8
@export var spin_time := 2.0
@export var spin_count := 1
var spinning := false


func spin(to_segment: int) -> void:
    if spinning:
        return
    spinning = true
    print(to_segment)
    randomize()
    var d := DEGREES / segment_count
    var r := d / 2
    var t := (to_segment-1) * d - r
    t += fposmod(wheel.rotation_degrees, DEGREES)
    #t += randf_range(0, d)  # add slight offset
    var target := wheel.rotation_degrees - (spin_count * DEGREES) - t

    var tween := create_tween().set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_OUT)
    await tween.tween_property(wheel, "rotation_degrees", target, spin_time).finished
    spinning = false


func _input(event: InputEvent) -> void:
    if event is InputEventMouseButton:
        if event.pressed:
            spin(randi() % segment_count + 1)  # pick random segment

Source Link

How spin clocwise wheel in Godot Engine?

Hello how spin roulette wheel in clockwise direction using tween? I've successfully managed to rotate counter-clockwise to exact segment, but clockwise direction is problem.

roulette wheel

extends Control


const DEGREES := 360.0
@onready var wheel := $Front as TextureRect

enum Directions {CCLOCKWISE=-1, CLOCKWISE=1}
@export var direction := Directions.CLOCKWISE
@export var segment_count := 8
@export var spin_time := 2.0
@export var spin_count := 1
var spinning := false


func spin(to_segment: int) -> void:
    if spinning:
        return
    spinning = true
    print(to_segment)
    randomize()
    var d := DEGREES / segment_count
    var r := d / 2
    var t := (to_segment-1) * d - r
    t += fposmod(wheel.rotation_degrees, DEGREES)
    #t += randf_range(0, d)  # add slight offset
    var target := wheel.rotation_degrees - (spin_count * DEGREES) - t

    var tween := create_tween().set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_OUT)
    await tween.tween_property(wheel, "rotation_degrees", target, spin_time).finished
    spinning = false


func _input(event: InputEvent) -> void:
    if event is InputEventMouseButton:
        if event.pressed:
            spin(randi() % segment_count + 1)  # pick random segment