• 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 InfamousStudios · Dec 14, 2013 at 02:16 AM · rigidbodymousedragclick to movedragrigidbody

Drag Rigidbody

I consulted search engine after search engine, forum after forum. I just can't find a suitable answer to this question!

Is there a script that I can use, which allows the player to drag a rigidbody when it is clicked and dragged? I need this for a game, which you need to drag objects to create a bridge, allowing gravity to rotate it naturally. I don't need to rotate it, just be able to drag it. Thanks in advance!

Comment
Add comment · Show 3
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 Spinnernicholas · Dec 14, 2013 at 02:43 AM 0
Share

Good luck finding a script. It depends a lot on how your game is setup and therefore, a generic script is probably not an option. If you did find one, it would probably be big and bulky and near impossible to use. But, it shouldn't be too hard to write your own script.

The script will need a drag state(ie: bool isDragging). You will need to raycast from the mouse position on the camera when the player clicks. The point where the ray hits the objects collider will be the object's rigidbody's center of mass during the drag. That way, it will rotate around the drag point.

It may be useful to have a control script to handle input,raycasting, etc. It can also store the object currently being dragged.

avatar image Spinnernicholas · Dec 14, 2013 at 02:51 AM 0
Share

From looking at your profile, none of your 15 questions have selected answers. Either none of them have good answers or you are forgetting to select them. If any of them have decent answers, please remember to select them.

avatar image robertbu · Dec 14, 2013 at 03:27 AM 0
Share

Have you tried the DragRigidbody.js script that comes with Unity? You can get it by Assets > Import Package > Scripts. If you've tried it, how is the behavior you want different from this scripts behavior?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ArchAngelus · Feb 15, 2014 at 07:23 PM

This was answered here: http://forum.unity3d.com/threads/56460-Drag-Rigidbody-script-works-but-passes-through-collider

If DragRigidBody doesn't work for what you need, try using Erich5h5's DragObject script: http://wiki.unity3d.com/index.php?title=DragObject

I'm actually going to attempt this myself now.

Comment
Add comment · 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
1

Answer by MegaMagaretha · Jan 27, 2016 at 05:43 AM

Here, i wrote a very simple script for dragging any Object with the rigidbody component. I hope i could help you.

using UnityEngine; using System.Collections;

public class DragRigidbody : MonoBehaviour {

 public float catchingDistance = 3f;

 bool isDragging = false;
 GameObject draggingObject;

 // Use this for initialization
 void Start()
 {

 }

 // Update is called once per frame
 void Update()
 {

     if (Input.GetMouseButton(0))
     {
         if (!isDragging)
         {
             draggingObject = GetObjectFromMouseRaycast();
             if (draggingObject)
             {
                 draggingObject.GetComponent<Rigidbody>().isKinematic = true;
                 isDragging = true;
             }
         }
         else if (draggingObject != null)
         {
             draggingObject.GetComponent<Rigidbody>().MovePosition(CalculateMouse3DVector());
         }
     }
     else {
         if (draggingObject != null)
         {
             draggingObject.GetComponent<Rigidbody>().isKinematic = false;
         }
         isDragging = false;
     }
 }



 private GameObject GetObjectFromMouseRaycast()
 {
     GameObject gmObj = null;
     RaycastHit hitInfo = new RaycastHit();
     bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
     if (hit)
     {
         if (hitInfo.collider.gameObject.GetComponent<Rigidbody>() &&
             Vector3.Distance(hitInfo.collider.gameObject.transform.position,
             transform.position) <= catchingDistance)
         {
             gmObj = hitInfo.collider.gameObject;
         }
     }
     return gmObj;
 }

 private Vector3 CalculateMouse3DVector()
 {
     Vector3 v3 = Input.mousePosition;
     v3.z = catchingDistance;
     v3 = Camera.main.ScreenToWorldPoint(v3);
     Debug.Log(v3); //Current Position of mouse in world space
     return v3;
 }

}

Comment
Add comment · Show 1 · 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 wolfoxdraws · Jan 17, 2018 at 03:11 PM 0
Share

yes! this is really helpful, but i have a question. is there a way to let the object your dragging still collide with things? its just going through. sorry, i know this is kind of old.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 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

Related Questions

How to properly convert mouse pos to world pos 1 Answer

newbie: drag a rigidbody with mouse 2 Answers

DragRigidbody - move object in Local Space, not World Space 1 Answer

2D Rigidbody Drag with Mouse 0 Answers

"Launch" a rigidbody 3 Answers


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