• 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 DoctorWhy · Dec 07, 2012 at 12:58 AM · positionpivotvector 3

Move gameobject relative to mouse cursor location

I have a gameobject that can be moved in a 3D space. Depending on where the model was clicked (basically, the hit.point of a RaycastHit), I want it to move along with the mouse.

So I can get the model to move to the position based on its own pivot point

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 Vector3 PositionToMoveTo = ray.GetPoint(5);
 gameObject.transform.position = PositionToMoveTo;

But I want to then adjust for the position on the part of the model that was hit by the ray, so the hit.point is over the PositionToMoveTo, instead of just the gameObjects pivot. How would I adjust its position?

EDIT: Screenies

![alt text][1] [1]: /storage/temp/5579-example1.jpg

The red dot is the position that was clicked. The green position is where the objects pivot is (so its transform.position).

![alt text][2] [2]: /storage/temp/5580-ex.png

Wherever the mouse is, the red dot should be right underneath. The model does not rotate with the camera, but the position should move with the mouse. The green dot (the position/pivot of the model), should not be underneath the mouse. But with the code above, that is what it currently is.

ex.png (9.8 kB)
example1.jpg (21.5 kB)
Comment
Add comment · Show 9
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 MountDoomTeam · Dec 07, 2012 at 01:56 AM 0
Share

I don't really understand the question, the hit.point is a position in space, and the pivot is related to the rotation of the moving object. If you want the rotation to happen above the target point, what kind of rotation do you want from the moving object? the question is really vague.

avatar image DoctorWhy · Dec 07, 2012 at 02:06 AM 0
Share

I don't really see how it is vague, but I understand that it might not be that clear. The pivot also has to do with the position. So when you set the position of the gameobject, you are setting the location based on the pivot of the object (which can be changed so it isn't the center). When I set the position of the gameobject, I want to offset the position by the hit.point location, so the location of hit.point is actually where the positionTo$$anonymous$$oveTo is

avatar image MountDoomTeam · Dec 07, 2012 at 02:28 AM 0
Share

in geometry, rotation and location are different and separate distinct operations, and you couldn't change the position of an object using a rotatione, except-if you are rotating around a point which is different from the position of the object that is moving, in which case you have to explicitly state in code what the pivot points should be. normally unity you will change the position of something in one sense, or rotate the position in another sense, you can use rota$$anonymous$$round command http://answers.unity3d.com/questions/62059/rotate-around-a-point.html . to set of pivot point for an object, you have to explicitly state that you want to rotate an object around a point in space, in which case you're not talking about the game objects pivot, but a pivot point, that you have to decide, that is separate from the object. To make the question easier you should say how you want the object to rotate, I read your question about the mouse following and I still didn't get a very clear idea, perhaps you should take screenshots and put them on the questions?

avatar image DoctorWhy · Dec 07, 2012 at 02:40 AM 0
Share

I want the position of the place you clicked on a mesh collider to be the exact position that follows the mouse. In other words, if I click a position on the head of a humanoid model, the gameobject should now be following the mouse with the point you clicked on the head, underneath the mouse cursor. The game object does not need to rotate at all. Just the position matters. I will get you screenshots if you really need them...

avatar image MountDoomTeam · Dec 07, 2012 at 03:02 AM 0
Share

the gameobject should now be following the mouse with the point you clicked on the head, underneath the mouse cursor.

following object should follow the head, underneath the mouse cursor? what is the following object?

what if the mouse cursor moves seperately to the head? then the object is related to the head and the mouse? the head follows the mouse? its still confusing to me.

its viewing from above areas i figure.

avatar image MountDoomTeam · Dec 07, 2012 at 03:03 AM 0
Share

you are brother of doctor who lol

avatar image DoctorWhy · Dec 07, 2012 at 03:17 AM 0
Share

Sorry, the head of a humanoid model. And the camera can be at any angle (well, Z is always 0, I think). So above, below, to the side, behind, any diagonal, etc.

avatar image DoctorWhy · Dec 07, 2012 at 04:01 AM 0
Share

I edited and further explained it and added screenshots, though they are not appearing for me above (but are when editing), so you will have to click the images to see them.

avatar image MountDoomTeam · Dec 07, 2012 at 08:13 AM 0
Share

I figure what you want, is to be able to click on the head of the humanoid, and then move the mouse, and the head will stay stuck to the mouse cursor position. You also have to state what kind of plane you want the humanoid to move in, in the formulas, because the mouse and the can decide 2 dimensions and not depth. You said it doesn't matter the camera angle, so for the moment we can use the plane facing the camera for the plane horizontal compared to the humanoid.

1st of all, you have to deter$$anonymous$$e the point clicked, compared to the object position, and make it a variable.

modelpointclicked = world hit position - hit GameObject position;

You should also become familiar with different mouse clicking options, because the one you selecting works at exactly 5 metres:

Raycast.point will give you the point of collision of model at any distance

so then, you have mesh point click which should be something like 1 m 70 above the humanoid transform position, and so you are going to use hipsGameObjectposition + modelpointclicked as the point that will follow your mouse cursor.

I'm not even sure how to make something follow the mouse cursor! I would have to look it up, but I hope that helps.

1 Reply

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

Answer by MountDoomTeam · Dec 07, 2012 at 08:14 AM

I figure what you want, is to be able to click on the head of the humanoid, and then move the mouse, and the head will stay stuck to the mouse cursor position. You also have to state what kind of plane you want the humanoid to move in, in the formulas, because the mouse cursor can decide 2 dimensions and not depth. You said it doesn't matter the camera angle, so for the moment we can use the plane facing the camera for the plane or horizontal compared to the humanoid.

1st of all, you have to determine the point clicked, compared to the object position, and make it a variable.

modelpointclicked = world hit position - hit GameObject position;

You should also become familiar with different mouse clicking options, because the one you selecting works at exactly 5 metres:

 function MouseSelect() {
     if ( Input.GetMouseButtonDown(0) && Input.GetKey("z"))
     {
         var raypos : Vector3;
         var hit : RaycastHit;
         var dist : float = 220;
         var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         if (Physics.Raycast (ray, hit, dist))
         {
         var rayposobj = hit.collider.transform.position;
                              //or
             raypos = hit.point
 //http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-point.html
         print( "raypos" + raypos );
 var relativehitpt = rayposobj - raypos;
 //get mouse to follow relativehitpt
     }
 } 

Raycast.point will give you the point of collision of model at any distance

so then, you have mesh point click which should be something like 1 m 70 above the humanoid transform position, and so you are going to use hitGameObjectposition + modelpointclicked as the point that will follow your mouse cursor.

also you can trick the mouse cursor follow script to provide a following point 1 m 70 under the actual visible mouse position.

I'm not even sure how to make something follow the mouse cursor! I would have to look it up, but I hope that helps.

Comment
Add comment · Show 2 · 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 DoctorWhy · Dec 07, 2012 at 03:00 PM 0
Share

The code I have above makes the object follow the mouse (at a consistent depth, so it looks like the object is getting bigger on the sides of the screen since it is moving in a radius type of movement, which is what my other question was about). The vector math is what I don't know. I will see if this works and get back to you.

avatar image DoctorWhy · Dec 07, 2012 at 05:14 PM 0
Share

A slightly modified version of this (in C#) worked. Thank you.

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

10 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

Related Questions

center of object group 0 Answers

How do you get the world-space position of a sprite with a custom pivot? 1 Answer

changing pivot's placement 2 Answers

Instantiated Game Object is at the wrong location. 1 Answer

Camera rotation around player while following. 6 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