Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Harryliu · May 17, 2016 at 12:23 AM · vector3mousemouseclick

Moving a object along a plane?

I have a script casts a ray from where the mouse has clicked and attaches the object to the origin of the ray. However, when I move the mouse I want the object to move along the X and Z axis( along a plane ) instead of along the Y axis. How can I do that?

Thanks

Maybe a photo will explain better.alt text

Here's the code: function OnMouseDown () {

     if(!selected)
     {
         selected = true;
     }
 
     else
     {
         selected = false;
     }
 }
 
 function Update () {
 
     if(selected == true)
     {
         FollowCursor();
     }
 }
 
 function FollowCursor()
 {
     var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var point : Vector3 = ray.origin + (ray.direction * distance);
     obj.transform.position = point;
 }
unity-question.png (54.8 kB)
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 instruct9r · May 17, 2016 at 01:38 AM 0
Share

Can you show the code, so somebody can modify it, ins$$anonymous$$d of writing it from scratch...

avatar image Harryliu instruct9r · May 17, 2016 at 01:57 AM 0
Share

I added the code to the question. Formatting is weird here.

avatar image DaniruKoresan · May 17, 2016 at 01:40 AM 0
Share

Agreed gonna need that code, I too would like to learn to do this so I'll follow.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eno-Khaon · May 17, 2016 at 03:03 AM

You should probably look at some of the examples for Physics.Raycast() and, for a more closely connected example for what you're doing, Input.mousePosition.

An easy aspect to overlook when using the conversions between screen space and world space (among others) with the camera is the lack of depth.

When you get the mouse position, the value is a Vector3, yet it generally provides no significant/usable data on the Z-axis. As a result, converting from the mouse position on screen to a world point can do so relative to the camera's perspective, but will only be able to utilize the depth assigned to it manually.

To compensate for that, you can instead fire a Raycast. This can be used to hit at the position of the mouse cursor on the screen -- Then you have an accurate point of reference for where to move to.

 function FollowCursor()
 {
     var ray : Ray = Camera.main.ScreenPointToRay    (Input.mousePosition);
     var hit: RaycastHit;
     if(Physics.Raycast(ray, hit, 100))
     {
         var point : Vector3 = hit.point;
         obj.transform.position = point; // No smoothing
     }
     obj.transform.position = point;
 }

If you don't make any other changes first, something rather silly will happen. The Raycast will hit the ground and the box will move to a position where it's halfway embedded in the ground. Then, on the next frame, the Raycast will instead hit the box and the box will move to where the cursor was aimed at the box. On the next frame, the box will move further towards the screen. This will occur until the box passes through the camera, clearing the way for the Raycast to hit the ground again.

Something needs to be done about that.

Well, there are two relatively easy options to choose from to make this work smoothly:

1) Rather than using Physics.Raycast, use Collider.Raycast(). If the Raycast is linked directly to the ground, then it will be unable to hit anything else, but this will keep the box from being a potential Raycast target.

2) Put the box on its own layer and have the Raycast ignore that layer (by adding another argument into the overloaded function). This is the potentially-more-versatile option. It won't be limited to a single object's collider like option 1, but it will require more care to prevent the raycast from hitting any unintended targets.

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
0

Answer by troien · May 17, 2016 at 11:46 AM

In theory, you actually don't have to raycast at all to move along a imaginary plane. Just make sure you set the distance in your calculation to the correct value.

If your plane is y up like in your image, all you'd have to do is change the "distance" in your calculation to ((ray.origin.y - plane.y) / ray.direction.y) and you are done :p (except if ray.direction.y is 0, but in theory you should prevent such values by placing your camera correctly)

Another way to do this is to use Plane.Raycast. Added advantage is that this also works for planes that don't have y-up as their normal and it catches the times when the ray is moving parallel to the plane (the ray.direction.y == 0 in the example above) or the ray is moving away from your plane by simply returning false rather then true, like so:

 Plane plane = new Plane(Vector3.up, Vector3.zero);
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 float distance;
 if (plane.Raycast(ray, out distance))
 {
     Vector3 pointalongplane = ray.origin + (ray.direction * distance);
 }

Using Ray.GetPoint like in the example looks even fancier, but yea, does the same :p

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
0

Answer by Yeowza · Dec 11, 2021 at 11:24 PM

I am using the same code and it is exactly what I need to do but is there a way to make it so wherever you click on the object it will drag from that point instead of in the center?

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 Yeowza · Dec 12, 2021 at 03:03 PM 0
Share

Maybe this will help someone, https://forum.unity.com/threads/unity-3d-drag-game-board-with-mouse.1210806/

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

53 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

Related Questions

MouseOver different Objects C# 1 Answer

position.Set vs. position = new Vector3() 0 Answers

Mouse plane does not detect height 1 Answer

Mouse Over & Mouse Click 1 Answer

Causing animaton on mouseclick? 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