Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Sylvine · May 26, 2013 at 08:21 PM · collisionmouse-drag

How to deal with collision by using mouse drag?

I'm doing a 15-puzzle game. It's probably not the most clever way to do but I'm using RigidBody for the cubes. The player moves the cube by dragging them with the mouse.

The problem is no matter what I do, one of the cube will eventually move through another one. All the cubes use continuous collision detection and I'm using MovePosition, but it still don't work.

Here's my function (beware, it's crappy code) :

 function OnMouseDrag(){
     var hit : RaycastHit;
         //keep track of the mouse position
         var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
 
         //convert the screen mouse position to world point and adjust with offset
         curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
              if (curPosition.x - 0.1 > transform.position.x ){
                 if(rigidbody.SweepTest (transform.right, hit, 0.02) == false){
       rigidbody.MovePosition(rigidbody.position + Vector2.right * (1) * Time.deltaTime);
      }
         }
         else if (curPosition.x +0.1 < transform.position.x){
         if(rigidbody.SweepTest (transform.right, hit, -0.02) == false){
       rigidbody.MovePosition(rigidbody.position + Vector2.right * (-1) * Time.deltaTime);
      }
         }
         else if (curPosition.y -0.1 > transform.position.y){
          if(rigidbody.SweepTest (transform.up, hit, 0.02) == false){
       rigidbody.MovePosition(rigidbody.position + Vector2.up * (1) * Time.deltaTime);
      }
         }
         else if (curPosition.y +0.1 < transform.position.y){
         if(rigidbody.SweepTest (transform.up, hit, -0.02) == false){
       rigidbody.MovePosition(rigidbody.position + Vector2.up * (-1) * Time.deltaTime);
      }
         }
 
     }
Comment
Add comment · Show 5
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 robertbu · May 26, 2013 at 08:39 PM 0
Share

From the Assets menu, select Import Package/Scripts. Then play with the DragRigidbody script that comes in as part of this package. It uses joints and does not have the issue you have by using $$anonymous$$ovePosition().

avatar image Sylvine · May 27, 2013 at 12:34 AM 0
Share

That's what I used first, I don't remember why I scraped it, anyway the problem is the same, the collision are even more jiggly. Just to be sure, I just have to apply the script on all the rigidbody I wan't to move and that's it ?

Also I forgot to say that I need the objects to be kinematic, I don't want them to move when the player is not touching them.

avatar image Sylvine · May 27, 2013 at 12:50 AM 0
Share

Ok, I've managed to bypass the kinematic limit with this :

     function On$$anonymous$$ouseDown(){
     this.rigidbody.is$$anonymous$$inematic = false;
     }
     
     function On$$anonymous$$ouseUp(){
     this.rigidbody.is$$anonymous$$inematic = true;
     }


But now I'm back from the start, I can still make objects go trough each other.

avatar image robertbu · May 27, 2013 at 03:35 PM 0
Share

DragRigidbody is applied to only one game object. Create an empty game object and attach it. It will work for all the rigidbodies in the scene. Note is is fairly straight forward to modify this script so that it will turn the iS$$anonymous$$inematic flag on and off so you will not have to add the script above to all your objects. You may have to alter the spring values and the attach point to get the drag behavior to be what you want.

Note by default object will inter-penetrate slightly. If this is the issue you are trying to work around, go to Edit/Project Settings/Physics, and '$$anonymous$$in Penetration For Penalty' value to 0.0.

avatar image Sylvine · May 27, 2013 at 07:40 PM 0
Share

Editing the project setting greatly improve this. Thank you !

0 Replies

· Add your reply
  • Sort: 

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

13 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

Related Questions

Collision with object using MouseDrag 1 Answer

Throw an object on collision with mouse cursor 1 Answer

Dragging Rigidbodies goes through colliders 3 Answers

Dragging Rigidbodies goes through colliders on non linear paths. 2 Answers

How to move objects away from each other (not what you think) 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