- Home /
can I drag out the scale of a plane?
hey all,
Im trying to have an effect where the player will click on the screen, and while holding the mouse down, they are able to drag out an arrow shape which points at the position where the mouse click was 1st made.
i know id need the positions of the mouse click and the currrent position, but im not sure how to instantiate a plane which pointed towards the startPOS while the rear end is linked to currentPOS, thats if its even possible?
Answer by robertbu · Aug 07, 2013 at 12:47 AM
The Vectrosity package in the Asset store makes drawing lines and arrows easy. Depending on what you are doing, it may be a good solution.
Here is another solution. To test it:
Get the CreatePlane script from the Wiki.
Create a 1 unit Vertical plane
Put whatever material you want on the plane. Note the point of the arrow should be up. You will have to do the arrowhead as a separate object if you don't want it stretched.
Attach this script
Set the x in the scale to .001.
Hit play and click and drag.
#pragma strict
private var pos1 : Vector3;
private var pos2 : Vector3;
var objectHeight = 1.0;
function Update () {
if (Input.GetMouseButtonDown(0)) {
pos1 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
pos1 = Camera.main.ScreenToWorldPoint(pos1);
pos2 = pos1;
}
if (Input.GetMouseButton(0)) {
pos2 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
pos2 = Camera.main.ScreenToWorldPoint(pos2);
}
if (pos2 != pos1) {
var v3 = pos2 - pos1;
transform.position = pos1 + (v3) / 2.0;
transform.localScale.y = v3.magnitude;
transform.rotation = Quaternion.FromToRotation(Vector3.up, v3);
}
}
thankyou, howevere that didnt seem to work, the x scale stayed on .001 and ony the y axis was changing as a dragged :S
$$anonymous$$eep this x axis the same was the intent. The code was for drawing lines, so you use a line texture and can draw out an "arrow". But if you want to scale both x and y, just insert the following line between line 25 and 26 above:
transform.localScale.x = v3.magnitude;
that actually is pretty much bang on! thankyou.. the only problem i have left with it is its tilted when i drag it out, what the best way fro me to fix it so its locked flat parallel to the floor
I changed a couple of lines. $$anonymous$$aybe this is what you are looking for:
private var pos1 : Vector3;
private var pos2 : Vector3;
var objectHeight = 1.0;
function Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0)) {
pos1 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
pos1 = Camera.main.ScreenToWorldPoint(pos1);
pos2 = pos1;
}
if (Input.Get$$anonymous$$ouseButton(0)) {
pos2 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
pos2 = Camera.main.ScreenToWorldPoint(pos2);
}
if (pos2 != pos1) {
var v3 = pos2 - pos1;
transform.position = pos1 + (v3) / 2.0;
transform.localScale.y = $$anonymous$$athf.Abs(v3.y);
transform.localScale.x = $$anonymous$$athf.Abs(v3.x);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Click and drag a plane 1 Answer
Unity2D - Box Collider 2D problem 0 Answers
Need a little help in script 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers