- Home /
Resize / Scale, Drag, Move a Plane at Runtime by Mouse action
Is it possible to find the Coordinates of the mouse with respect to the GameObject (in this case, a PLANE). If it is near the edge, then give the Resize option where you can drag the mouse diagonally to Scale it UP or Down. If the mouse is in Center, then give the option to Drag the object around.
I have the dragging part figured out using the OnMouseOver() , however am trying to detect when the Mouse is near the Edge of the plane.
In short- I want to select a Plane, either move it or resize it based on whether the User clicks in the Center or Edge.
Answer by Jesse Anders · Sep 07, 2010 at 09:13 PM
Yes, it is possible.
To address the specific question of differentiating between a 'move' operation and a 'resize' operation, you'll want to transform the pick point into the local space of the plane. (Note that, strictly speaking, we're talking about a rectangle here and not an actual plane.)
To transform the point into local space, you can use Transform.InverseTransformPoint(). There are several ways you could then determine whether the point is near an edge. A simple solution would be to compare the absolute values of the transformed point's x and z elements (assuming the plane lies in the XZ plane in local space) to a threshold value. For example, if the plane (rectangle) has an x and z extent of 10, you would consider the point as being close to an edge if abs(p.x) or abs(p.y) is greater than (e.g.) 9. (You can also use this information to determine which edge the point is nearest to.)
Thanks a lot. Let me try that out. So if I have 10 different planes(rectangles), can I attach the same script to all of them and then have them function the same way? or will I be tied down with 1 single plane and Camera position w.r.t that object.
Assu$$anonymous$$g the implementation is correct, you can attach the same script or scripts to all of the rectangles in your scene. There should be no need to duplicate the functionality on a per-object or per-camera basis.
Answer by Ben 16 · Sep 28, 2010 at 02:03 PM
Can I ask how you did your resizing, as I am faced with a similar task just now. The way I intend to tackle the identifying of where the user clicks is actually create non rendered game objects around the edges of the object, so that in the corners I can say "scale uniformly" and on the edges "scale in direction (x||y||z)" while if the mouse is over the main game object, drag to move. So I have my concept, just need to work out how to resize an object at runtime based on a mouse drag.
If you are just looking for resize, you can have a look at some of the Gizmo examples posted by other users. Check this one - http://answers.unity3d.com/questions/5275/how-to-create-handles-like-editor-gizmos-in-runtime