Mouse Position Problem

I'm trying to figure out a work around for a mouse click issue i've run into. I'm using the code below to move game objects around inside OnMouseDrag().

myTransform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 175.0f));

My issue is if I click on corners of the game object the object centers to the mouse and I'm trying to figure out if there is a way to center the click at the center of the game object. It sometimes causes the objects to shoot off in a direction that wasn't intended and its making the game play kind of frustrating.

Any ideas would be appreciated... =)

1 Answer

1

If I understand this correctly it has some kind of dragging going on.

Couldn't you do something like this:

if Input.GetButtonDown(KeyCode.Mouse0)
        mouseOffset=myTransform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 175.0f));

And add the mouseOffset everytime you update the new position.

Does that solve the problem?

I will give this a try when I get home... The problem is when I first click the game object right before I drag it... it centers to the mouse but the drag works fine. It just snaps to the center of the mouse sometimes colliding with objects around it that I didn't want it to hit.

I think this should compensate for that

Ok, cool... I just have one more question am I setting myTransform.position = myTransform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 175.0f)); The mouseOffset variable is kinda throwing me off...

myTransform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 175.0f))+mouseOffset; And you would ofcourse need to declare private var mouseOffset : Vector3; in the beginning of the script

This totally did the trick thanks a ton dude!