Hello, I’m new to Unity and I’m making my first game ever in this engine.
I currently have a 3 dimensional grid of positions for cubes, and they must move 1 position at a time in order to form a path with some faces, similar to a rubiks cube but only with translation, no rotation.
I have implemented a RayCast to select with a click which cube to move (cube movement through keypad (awsd))
but I have made a mistake! I need that once one cube is selected, the player has enough time to move the cube, since right now I have to click and press the keys very fast for it to kinda-work. Is there a way to adapt this code for the game to wait until the player moves the cube and “hold” the selection?
This is the click script I have attached to the camera to select a cube to move
Script: Click
[SerializeField]
private LayerMask clickableLayer;
void Start()
{
}
void Update()
{
if(Input.GetMouseButtonDown(0))
{
RaycastHit rayHit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rayHit, Mathf.Infinity, clickableLayer))
{
Debug.DrawRay(transform.position, transform.forward, Color.green); print("Hit");
rayHit.collider.GetComponent<block>().moveIt();
}
}
And I have another script attached to each cube, with the method that the click calls.
Script: Block
void Update()
{
if (currentlySelected)
{
moveIt();
}
}
public void moveIt()
{
myRender.material = matSelect;
while (set != true)
{
if (GameObject.Find(x.ToString() + y.ToString() + z.ToString()).GetComponent<cubo>().free == true)
{
transform.position = GameObject.Find(x.ToString() + y.ToString() + z.ToString()).GetComponent<cubo>().transform.position;
GameObject.Find(x.ToString() + y.ToString() + z.ToString()).GetComponent<cubo>().free = false;
set = true;
}
else
{
RandomPos();
}
}
if (Input.GetKeyDown(KeyCode.W)) // x dimension
{
if (GameObject.Find((x + 1).ToString() + y.ToString() + z.ToString()).GetComponent<cubo>().free == true)
{
transform.position = GameObject.Find((x + 1).ToString() + y.ToString() + z.ToString()).GetComponent<cubo>().transform.position;
GameObject.Find(x.ToString() + y.ToString() + z.ToString()).GetComponent<cubo>().free = true;
GameObject.Find((x + 1).ToString() + y.ToString() + z.ToString()).GetComponent<cubo>().free = false;
x++;
}
//and same thing with the other dimensions, this is getting kind of long
}
myRender.material = matStand;
currentlySelected = false;
}
private void RandomPos()
{
x = Random.Range(0, 3);
y = Random.Range(0, 3);
z = Random.Range(0, 3);
}