- Home /
 
Need help selecting a game Object to move it
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);
     }
 
              Answer by Anis1808 · Dec 06, 2019 at 04:12 AM
All you have to do is change this
 rayHit.collider.GetComponent<block>().moveIt();
 
               To this
 rayHit.collider.GetComponent<block>().currentlySelected = true;
 
               And make the variable currentlySelected public so the Click script can access it.
Your answer
 
             Follow this Question
Related Questions
click first, click second, destroy if matched 1 Answer
selected object in array lost in translation 0 Answers
Some Copies of Prefab Detect Clicks But Others Don't 1 Answer
Is there a way to click on a render texture to select soemthing within the view? 4 Answers
GameObject becomes 'unselectable' 1 Answer