And man, is it HARD. There’s no ‘corner’ value that can be modified to lead the shape of the rectangle. Does anyone have any quick tips for doing this? I’ve seen some online tutorials, but they’re all for 3d, and I’m using 2d. Here’s what I’ve got so far. It’s a bit of a mess. It does create a rectangle which vaguely follows the cursor, but the starting corner definitely doesn’t stay put, and corner opposite it definitely doesn’t stick to the mouse. I get the feeling that this is completely the wrong approach:
public class UnitSelection : MonoBehaviour {
public GameState gameState;
float downTime = 1000000;
enum relativeMousePlaces {upRight, downRight, downLeft, upleft};
relativeMousePlaces mouseVsStart;
Vector2 mousePosLastFrame;
Vector2 mouseDownLocation;
public Transform quadrangle;
Transform selectorSquare = null;
void Awake() {
gameState = gameObject.GetComponent<GameState>();
quadrangle.gameObject.SetActive(false);
}
void Update() {
if (Input.GetKeyDown(KeyCode.Mouse0)) {
downTime = Time.time;
mouseDownLocation = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePosLastFrame = mouseDownLocation;
}
else if (Input.GetKeyUp(KeyCode.Mouse0)) {
downTime = 1000000;
selectorSquare.gameObject.SetActive(false);
selectorSquare = null;
}
else if (Time.time - downTime >= 0.1) {
if (selectorSquare == null) {
selectorSquare = quadrangle;
selectorSquare.gameObject.SetActive(true);
Debug.Log(selectorSquare.gameObject.activeSelf);
}
else {
float xDelta = (Input.mousePosition).x - (mousePosLastFrame).x;
float yDelta = (Input.mousePosition).y - (mousePosLastFrame).y;
selectorSquare.localScale += new Vector3 (xDelta, yDelta, 0);
if (Input.mousePosition.x > mouseDownLocation.x) {
if (Input.mousePosition.y > mouseDownLocation.y) {
mouseVsStart = relativeMousePlaces.upRight;
}
else {
mouseVsStart = relativeMousePlaces.downRight;
}
}
else if (Input.mousePosition.y < mouseDownLocation.y) {
mouseVsStart = relativeMousePlaces.downLeft;
}
else {
mouseVsStart = relativeMousePlaces.upleft;
}
Vector3 changeMag = Camera.main.ScreenToWorldPoint(Input.mousePosition) - Camera.main.ScreenToWorldPoint((Vector3)mousePosLastFrame);
selectorSquare.position += changeMag;
}
mousePosLastFrame = Input.mousePosition;
}
}