• 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 Biendeo · Jun 13, 2011 at 03:50 AM · gameobjectmousedrag

Fixing "Drag GameObject with mouse" script

Hi! I'm making a sort-of 2D platformmer where you must drag certain GameObjects with the mouse to get through. I've got a script that allows this to happen, except for one problem. When you drag an object, it seems to try to snap to the centre of the screen, and it does not move exactly with the mouse. Instead, the object requires more mouse movement than it should. If this is hard to explain, you can try out this script:


 var normalCollisionCount = 1;
 
 var moveLimit = .5;
 var collisionMoveFactor = .01;
 var addHeightWhenClicked = 0.0;
 var freezeRotationOnDrag = true;
 var cam : Camera;
 private var myRigidbody : Rigidbody;
 private var myTransform : Transform;
 private var canMove = false;
 private var zPos : float;
 private var gravitySetting : boolean;
 private var freezeRotationSetting : boolean;
 private var sqrMoveLimit : float;
 private var collisionCount = 0;
 private var camTransform : Transform;
 
 function Update () {
     transform.rotation = Quaternion.identity;
 }
 
 function Start () {
     myRigidbody = rigidbody;
     myTransform = transform;
     if (!cam) {
         cam = Camera.main;
     }
     if (!cam) {
         Debug.LogError("Can't find camera tagged MainCamera");
         return;
     }
     camTransform = cam.transform;
     sqrMoveLimit = moveLimit * moveLimit;   // Since we're using sqrMagnitude, which is faster than magnitude
 }
 
 function OnMouseDown () {
     canMove = true;
     myTransform.Translate(Vector3.forward*addHeightWhenClicked);
     gravitySetting = myRigidbody.useGravity;
     freezeRotationSetting = myRigidbody.freezeRotation;
     myRigidbody.useGravity = false;
     myRigidbody.freezeRotation = freezeRotationOnDrag;
     zPos = myTransform.position.z;
 }
 
 function OnMouseUp () {
     canMove = false;
     myRigidbody.useGravity = gravitySetting;
     myRigidbody.freezeRotation = freezeRotationSetting;
     if (!myRigidbody.useGravity) {
         myTransform.position.z = zPos-addHeightWhenClicked;
     }
 }
 
 function OnCollisionEnter () {
     collisionCount++;
 }
 
 function OnCollisionExit () {
     collisionCount--;
 }
 
 function FixedUpdate () {
     if (!canMove) return;
    
     myRigidbody.velocity = Vector3.zero;
     myRigidbody.angularVelocity = Vector3.zero;
     myTransform.position.z = zPos;
     var mousePos = Input.mousePosition;
     var move = cam.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, camTransform.position.y - myTransform.position.z)) - myTransform.position;
     move.z = 0.0;
     if (collisionCount > normalCollisionCount) {
         move = move.normalized*collisionMoveFactor;
     }
     else if (move.sqrMagnitude > sqrMoveLimit) {
         move = move.normalized*moveLimit;
     }
    
     myRigidbody.MovePosition(myRigidbody.position + move);
 }
 
 @script RequireComponent(Rigidbody)


Can anyone tell me what I should add to the script in order to get it to work properly?

Here's the game if you wanna see what's the problem: http://biendeo.webs.com/WebPlayer.html

EDIT: I tried this same script, but using an orthographic perspective in the camera, and it works fine (although these draggable objects seem to go through the platform). Hopefully that'll help too.

Comment
Add comment · Show 2
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 Eric5h5 · Jun 13, 2011 at 04:01 AM 0
Share

The script works properly as-is; you don't need to add anything, given the limitation that you're dragging on the X/Z plane. It's from here: http://www.unifycommunity.com/wiki/index.php?title=DragObject , and you should link to that page ins$$anonymous$$d of re-posting the script. Follow the directions on that page and it works, as demonstrated by the link to the demo.

avatar image Biendeo · Jun 13, 2011 at 04:32 AM 0
Share

Here's an link to the game so far. From that script (which well spotted :D) to my script, all I changed were any instances of stuff on the y-axis to the z-axis, since the original script was looking down at a surface, I'm looking sideways at a wall. Here's what my problem is: http://biendeo.webs.com/WebPlayer.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by XtremeLeo · Jun 13, 2011 at 04:05 AM

Maybe as it's 2D, you limited the position of your GameObject via its rigidbody constraints. If you did, just uncheck the axis you want your GameObject to move.

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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How would you override Editor object dragging? 0 Answers

Restricting "Drag GameObject" script to Z Axis? 1 Answer

Drag GameObject only in a specific area 1 Answer

Controlling an animation by dragging a gameObject with the mouse 0 Answers

Dragging a GameObject with mouse 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