I’m making a scaleable cube, on the xyz faces there are handles which when dragged scale the central cube.
Using the below code works on the X(red) handle and with slight changes on the Y(green) handle.
import UnityEngine;
import System.Collections;
var screenPoint : Vector3;
var offset : Vector3;
var scalingCube : GameObject;
var clipBounds : Vector2;
function OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(this.transform.position);
offset = this.transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
}
function OnMouseDrag()
{
var curScreenPoint : Vector3 = Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
var curPosition : Vector3 = Camera.main.ScreenToWorldPoint(curScreenPoint)+ offset;
curPosition.x = Mathf.Clamp(curPosition.x, clipBounds.y, clipBounds.x); //clip the movement to a usable range
this.transform.position.x = curPosition.x; // move the handle
scalingCube.transform.localScale.x = (this.transform.localPosition.x * -2); // scale the centre cube
}
However I’m having trouble getting it working on the Z handle, I think it’s something to with moving the mouse to the right, I’ve altered to code to what’s below which almost works the only problem is when I click the handle it automatically jumps back to 0. (Note the -'s in front on Input.mousePosition.x’s)
import UnityEngine;
import System.Collections;
var screenPoint : Vector3;
var offset : Vector3;
var clipBounds : Vector2;
var scalingCube : GameObject;
function OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(this.transform.position);
offset = scalingCube.transform.position - Camera.main.ScreenToWorldPoint(Vector3(-Input.mousePosition.x, Input.mousePosition.y, 0));
}
function OnMouseDrag()
{
var curScreenPoint : Vector3 = Vector3(-Input.mousePosition.x, Input.mousePosition.y, 0);
var curPosition : Vector3 = Camera.main.ScreenToWorldPoint(curScreenPoint)+ offset;
curPosition.x = Mathf.Clamp(curPosition.x, clipBounds.y, clipBounds.x); //clamp the movement to a usable range
this.transform.position.z = curPosition.x; //set the handles position
scalingCube.transform.localScale.z = (this.transform.localPosition.z * -2); // -2 as we're scaling out the back of z
}
I guess it’s just one simple thing Im missing but have been staring at it for so long the codes turned to mush, any help would be much appreciated.