Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rahulpatil6220375 · Jul 09, 2019 at 06:29 PM · transformpositionmousedragcube

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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);
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rahulpatil6220375 · Jul 10, 2019 at 03:44 AM 0
Share

@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??

avatar image I_Am_Err00r rahulpatil6220375 · Jul 10, 2019 at 04:30 PM 0
Share

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.

avatar image rahulpatil6220375 I_Am_Err00r · Jul 10, 2019 at 04:48 PM 0
Share

@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

Show more comments
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rahulpatil6220375 · Jul 10, 2019 at 04:27 AM 0
Share

@bgoldbeck can i use raycasthit2d when collide a cube with each other In my scenario. i m making puzzle game 4*5??

avatar image bgoldbeck rahulpatil6220375 · Jul 10, 2019 at 04:47 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

160 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting to move object with mouse position? 0 Answers

Mouse move object along x axis 1 Answer

Drag object along Z axis using mouse 1 Answer

Get position of the closest visible vertex to mouse in scene view? 1 Answer

How to fix mouse click and drag hang in Unity 5.2.3.p2 OSX 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges