Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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
2
Question by didibus · Jul 07, 2010 at 05:23 PM · cameracoordinates

Have a gameObject follow the mouse in sync. So that the mouse pointer is always over the object. Even as the camera is rotated.

Hi,

So a couple weeks ago I asked a similar question here : http://answers.unity3d.com/questions/10268/have-a-gameobject-follow-the-mouse-in-sync-so-that-the-mouse-pointer-is-always-o

Today, I realized the solution to the problem wasn't so straightforward.

What I want is to be able to drag a cube around with the mouse, in a way that syncs perfectly with the mouse. The mouse cursor should always remain over the object, and visually, the object should move as much as the mouse did. This should work no matter what the rotation angle of the camera is. The cube should also be exactly under the point where we clicked, it shouldn't snap to its center.

Here is what I have that works, when the camera is looking perfectly perpendicular to the Z axis, but breaks when the camera is rotated or moved around at different angle:

private Vector2 snapOffset;

void OnMouseDown() { Vector3 temp = Camera.mainCamera.WorldToScreenPoint(transform.position);

 snapOffset = new Vector2(Input.mousePosition.x - temp.x, Input.mousePosition.y - temp.y);
 Debug.Log("snap offset: " + snapOffset);

}

void OnMouseDrag() { Vector3 mousePosition = new Vector3(Input.mousePosition.x - snapOffset.x, Input.mousePosition.y - snapOffset.y, 0); mousePosition.z = transform.position.z - Camera.mainCamera.transform.position.z;

 Debug.Log("mousePosition: " + mousePosition);

 transform.position = Camera.mainCamera.ScreenToWorldPoint(mousePosition);

}

This code works, if the camera is pointing straight at the cube from in front. If you rotate the camera, say 45 degrees, it will stop working.

I want a solution that does not use Ray-casting.

Thank you.

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

Answer by skovacs1 · Jul 07, 2010 at 10:58 PM

I think your question could use come clarification as to how you are moving your camera, but I think I get your meaning.

Have you considered something like:

Vector3 screenPosition;

//Store the screen position of the object when the mouse clicks void OnMouseDown() { screenPosition = Camera.mainCamera.WorldToScreenPoint(transform.position); }

//When the mouse drags, change the object's screen position accordingly. void OnMouseDrag() { //Read the mouse input axes screenPosition.x += Input.GetAxis("Mouse X"); screenPosition.y += Input.GetAxis("Mouse Y");

 //move the object to the world position to not change screen position
 transform.position = Camera.mainCamera.ScreenToWorldPoint(screenPosition);

}

This way the object is not centred to the mouse, but will keep its relative position on screen to where the mouse was clicked.

If the camera rotates in this script, then you can easily check if the mouse is held down and call transform.position = Camera.mainCamera.ScreenToWorldPoint(screenPosition); when the camera rotates

If the camera rotates in a separate script (which it likely does), I suggest making the relevant data public to the camera rotation so that it can update the object's transform if applicable (the mouse is still down). The exposed data could be the selected object and its stored screen position or a public function like updatePosition which will make the call to set the transform and then your rotate function and mousedrag can both call this.

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 Tetrad · Jul 07, 2010 at 05:56 PM

Try using something like Camera.ScreenPointToRay.

So i.e.

Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
transform.position = ray.GetPoint( distanceFromScreen );

Where distanceFromScreen is a fixed distance from the screen.

You'll have to use raycasting if you want to make sure your object doesn't go "behind" something, though. So you'd cast a ray, do a trace, and put your object's position at either what I had before, or the hit.point, whichever is closer.

Comment
Add comment · Show 6 · 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 didibus · Jul 07, 2010 at 06:21 PM 0
Share

Not sure what you mean by "going behind". Your method does work, but it snaps to the middle of the cube. Also, how do I find the distanceFromScreen? Doing the difference of the z position does not work when the camera is rotated say 45 degrees on the Y axis.

avatar image Tetrad · Jul 07, 2010 at 06:40 PM 0
Share

With that method, distanceFromScreen is just a "place this at this distance from the screen". If you want it to be a fixed amount you could get the magintude from the camera transform to the game object transform. i.e. (Camera.main.transform.position - transform.position).magnitude

avatar image didibus · Jul 07, 2010 at 06:59 PM 0
Share

Ya, I'd like my cube to be at the same distance it was from the camera as before, but finding the distance from the camera to the cube is behaving strangely. It's giving me 2 issues. The first one is, every time I click on the cube, it gets a little farther away from the camera. The second one is, it seems to be moving in a kind of arc, as you get to the edge of the screen, the cube gets closer to the camera, and when you are close to the middle, it gets farther.

avatar image Tetrad · Jul 07, 2010 at 07:16 PM 0
Share

Well that's how camera's work. It's technically the same distance, just not on the same plane.

avatar image didibus · Jul 07, 2010 at 07:30 PM 0
Share

If I move my mouse x amount to the left, the cube should move x unit to the left, while visually staying at the same distance from the camera. Say I move the mouse 10 pixel on the X axis. I first need to convert that in world unit. I can do this with ScreenToWorldPoint I think. This means that 10 pixel on the screen space is equivalent to lets say 100 unit in world space. So I then move the cube 100 unit on the X axis. But the problem is that when the camera is rotated. The world X axis is not parallel with the camera anymore. So I need a way to move 100 unit paralel with the camera clip plane

Show more comments

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Need help putting an object at a point based on screen coordinates 1 Answer

Backgrounds moving at different speeds? 1 Answer

Problems implementing a 3rd person static camera 0 Answers

How do you map touch points to world space? 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