x


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]: /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]: /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)
more ▼

asked Dec 07, 2012 at 12:58 AM

DoctorWhy gravatar image

DoctorWhy
310 67 70 97

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.

Dec 07, 2012 at 01:56 AM MountDoomTeam

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 positionToMoveTo is

Dec 07, 2012 at 02:06 AM DoctorWhy

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 rotatearound 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?

Dec 07, 2012 at 02:28 AM MountDoomTeam

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...

Dec 07, 2012 at 02:40 AM DoctorWhy

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.

Dec 07, 2012 at 03:02 AM MountDoomTeam
show all comments (comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Dec 07, 2012 at 08:14 AM

MountDoomTeam gravatar image

MountDoomTeam
1.2k 188 189 205

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.

Dec 07, 2012 at 03:00 PM DoctorWhy

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

Dec 07, 2012 at 05:14 PM DoctorWhy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1494
x143
x6

asked: Dec 07, 2012 at 12:58 AM

Seen: 3049 times

Last Updated: Dec 07, 2012 at 05:14 PM