- Home /
How To Drag any Cube Using Mouse Click and Moved Only 1 Unit??
i m making 2D puzzle game (4 * 5) see the referance.
i want to drag any cube using mouse click moved left,right,up,down only 1 unit should be moved.
i m writing code for 1 unit move cube in my program when user click mouse
 transform.position += Vector3.up * 0.1f;       
 transform.position += Vector3.down * 0.1f;
  transform.position += Vector3.left * 0.1f;
 transform.position += Vector3.right * 0.1f;
but i want to drag any cube for left,right,up,down moved(1 unit) .
how to drag any cube using mouse click left,right,up,down move 1 unit only.
Code:
  void Update()
     {
          bool up;
          bool down;
          bool left;
          bool right;
         if (Input.GetMouseButtonDown(0))
         {
             if (up == false)
             {
                 transform.position += Vector3.up * 0.1f;                    //only 1 unit move when drag any cube click in unity
                 down = true;
                 left = true;
                 right = true;
             }
            //else if (down == false)
             //{
             //    transform.position += Vector3.down * 0.1f;
             //    up = true;
             //    left = true;
             //    right = true;
             //}
             //else if (left == false)
             //{
             //    transform.position += Vector3.left * 0.1f;
             //    up = true;
             //    down = true;
             //    right = true;
             //}
             //else if (right == false)
             //{
             //    transform.position += Vector3.right * 0.1f;
             //    up = true;
             //    left = true;
             //    down = true;
             //}
 
         }
     }
 
Referance: https://imgur.com/a/VcqXKzN
Answer by I_Am_Err00r · Jul 09, 2019 at 09:09 PM
Best way to do this is define what a unit is, probably a float, and then when you click you do something like:
 transform.position += (Vector3.up + unit);
@skatesa207 thanks for answer 1 unit should be moved but problem is i want to how to drag a cube when mouse click ??help
my cube is moved 1 unit already but how to drag a cube when mouse click event??
Do mean move slowly by dragging (rather than just popping into position like transform.position would)? Try transform.translate:
// $$anonymous$$ove the object upward in world space 1 unit/second. transform.Translate(Vector3.up * Time.deltaTime, unit)
It will move over the time by the unit and simulate actually movement.
You can also do something similar with Vector3.$$anonymous$$oveTowards, Vector3.Lerp, Vector3.Slerp.
@skeatsa thanks for reply i m making https://en.wikipedia.org/wiki/$$anonymous$$lotski game
you can see my output when i m click mouse : https://imgur.com/a/qOs80kJ
when my cube reach the finish line(See above question image)then game over
player and cube moved 1 unit .. fine work
i want to player or cube only move when available only empty space.
i m implement a raycast to collide with to cube but not worked?
but problem is how to drag a player or cube when i m mouse click(left,right,up,down) ??help
Answer by bgoldbeck · Jul 10, 2019 at 04:06 AM
Unity has the OnMouseDrag() MonoBehaviour method, but you will need to have a collider attached to cube GameObject for it to detect the drag event.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDrag.html
@bgoldbeck can i use raycasthit2d when collide a cube with each other In my scenario. i m making puzzle game 4*5??
Still needing the collision component on the cube objects, you can use https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html to deter$$anonymous$$e cube colliding pairs.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                