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 Johnzee · Oct 13, 2010 at 07:20 PM · mouseaxisdragverticaly

How do I drag an object without Y transformation?

Hello -

I'm brand new at this Unity business and am just messing around with dragging objects and making them react to being "let go" and continue moving at the velocity of the drag. I found this thread: http://answers.unity3d.com/questions/4898/drag-gameobject-with-mouse which seems to do what I want but there's a lot I don't understand about it.

I set up a little game board with a top-down camera view and attached the DragRigidbody script to a sphere object. It works great for moving around the sphere, but I noticed that when I drag it nearer to the edges of the game screen the Y position of the sphere increases, as if I'm pulling the sphere up.

My question is: how can I modify the code to ONLY use the X and Z positioning of the mouse to determine the drag? In other words, I want the dragged object to stay on a 2D plane.

Some searching has led me to think that this is where the code needs to be changed (although I might be wrong):

while (Input.GetMouseButton (0))
{
    var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
    springJoint.transform.position = ray.GetPoint(distance);
    yield;
}

I know that Input.mousePosition gives me the coordinates of the mouse in 2 dimensions, so why am I still getting vertical movement?

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
2
Best Answer

Answer by skovacs1 · Oct 13, 2010 at 09:04 PM

Consider you camera to be a point in space. You are using a fixed distance around this point to generate rays. This gives you a sphere around that point.

Perspective cameras are projected according to their projection matrix (which is determined by their field of view) which will determine the skew of the view frustum and consequently a skew on the camera projection generated by ScreenPointToRay.

To get a plane parallel to the clip plane of the camera, you would either have to use an orthographic camera or use ScreenToWorldPoint with a fixed z coordinate.

If you were to change your code to this, it should give you motion along a plane parallel to the camera clip planes:

while (Input.GetMouseButton (0))
{
    var point = Input.mousePosition;
    point.z = distance;
    springJoint.transform.position = mainCamera.ScreenToWorldPoint(point);
    yield;
}
Comment
Add comment · Show 3 · 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 Johnzee · Oct 13, 2010 at 11:21 PM 0
Share

This works pretty well for the top-down view I was using. Thank you!

One more question: is it possible to modify the camera clip planes so I can angle the camera and still keep this code? Or do I need to try something else?

avatar image skovacs1 · Oct 14, 2010 at 04:35 PM 0
Share

Because ScreenToWorldPoint will get a point on a plane parallel to the camera's clip planes, moving/rotating the camera will change the plane on which points are returned. If that's what you want, then this code will work. If you want to always get a point on the same plane in space, regardless of your camera's position/rotation, then you should create a plane and raycast against it in s$$anonymous$$d.

avatar image Johnzee · Oct 16, 2010 at 03:15 PM 0
Share

Thank you very much, casting to the plane works great. Posting the code for others.

avatar image
1

Answer by Johnzee · Oct 16, 2010 at 03:26 PM

Skovacs1's answer worked but I thought I'd share the modified code here for others to see:

var castedPlane : Plane = new Plane(Vector3.up, Vector3.zero);
...
var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
//Start of the funky code
var ent : float = 100.0;
if (castedPlane.Raycast(ray, ent)){
    var hitPoint = (ray.GetPoint(ent) - Vector3(0, 1, 0));
    go.transform.position = hitPoint;
}
//End funky code
yield;

Where "go" is the object in question that I wanted to drag around (actually it was the Rigidbody dragger as referenced in DragRigidbody.js). Still had to subtract some height from the "hitpoint" in order for the object to go where I wanted (I think this is because the raycast is hitting the object's top, floating the true value upwards).

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 Aoldarion · Nov 26, 2016 at 02:18 PM 0
Share

Thanks for the plane raycasting code !

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

1 Person is following this question.

avatar image

Related Questions

Drag GameObject only in a specific area 1 Answer

Mouse look Y axis sensitivity doesn't change 0 Answers

Drag object along Z axis using mouse 1 Answer

Rotate and Move towards Mouse Point 0 Answers

On Mouse Drag Game Object follows Mouse on a single Axis. 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