- Edited
Hi, I started working on a rubik's cube simulator a few days ago. I have hit a few bumps along the way because I've just been guessing stuff and seeing what works. The idea is that each piece has their own number, so if each piece number is correct AND the rotation of each piece is (0, 0, 0) then it is solved. However, I ran into an issue with the tween I use to animate the turning.
I would like to "add" to the rotation degrees every time the cube is rotated, but with the current tween it only rotates once as the aim is already met after the first rotation. I understand that a variable could be stored to know which rotation it is on, but as it's a 3D puzzle I think that could get very messy. Does anyone know if there is a way to add to the rotation degrees instead of changing the Vector3 values? Help is much appreciated.
If you're a fellow cuber, you should realise this code is for a 2x2x2 Rubik's cube and not the conventional 3x3x3.
Here's my code for anyone that wants to see it:
extends Node3D
#the Piece nodes are CharacterBody3D nodes
@onready var pi1 = $Piece1
@onready var pi2 = $Piece2
@onready var pi3 = $Piece3
@onready var pi4 = $Piece4
@onready var pi5 = $Piece5
@onready var pi6 = $Piece6
@onready var pi7 = $Piece7
@onready var pi8 = $Piece8
#when I add this function, it will detect when variables match with the correct piece numbers and if those pieces are the correct orientation
var solved = true
var tween
func _ready():
solved = true
if solved == true:
pass
func _process(delta):
#I aim to add better controls later, but this will do for now
if Input.is_action_just_released("ui_left"):
rotate_green_cw()
#cw stands for clockwise
func rotate_green_cw():
var green_face = [
pi1,
pi3,
pi6,
pi8,
]
tween = get_tree().create_tween().bind_node(self).set_parallel().set_trans(Tween.TRANS_CUBIC)
for CharacterBody3D in green_face:
tween.tween_property(CharacterBody3D, "rotation_degrees", Vector3(0, 0, -90), 0.5)
tween.play()
#tmp stands for temporary and it means I can store the value of old pi6 temporarily before changing pi8 to the old pi6 value
var tmp = pi6
pi6 = pi1
pi1 = pi3
pi3 = pi8
pi8 = tmp