How to get local space coordinates without TransformPoint()?

Hi! I’m working on a project wich allows the user to build diffrent things. As a function in the program I want the user to be able to resize objects by doing as follows:

1.Clicking the object and thus highlighting it. (done)
2.Seing 5 small cubes appearing, one on each side of
the object and one on top. (done)
3.Being able to change the size of the object by
dragging these cubes along their local axis.

This is how I have thought of solving #3:
3-1.when the user pushes down the button → check where the cube is and where the cursor hits the cube using raycast.
3-2.check where the cursor is later and calculate the distance the cursor has been moved along the cubes local(!) axis.
3-3.move the cube this far along it’s local axis.

I want the cubes to follow the cursor but only along their given axis.

My problem is that i can’t figure out how to get the distance in 3-2. i can get the diffrence between the new cursor position and the first easy. But how to get it to local space for the cubes I don’t know.

Oh! The reason I’m having problems with this is because I can’t use transform.TransformPoint() since it’s is affected by scale and gets all buggy when I’m implementing the actual size change through transform.localScale.

I have been trying to use transform.TransformDirection but with no luck…

Thank you for taking your time! :slight_smile:

//The GO:s wich this script affects
private var targetObjectForScaling : GameObject;

//Var Declaration
var axis;
var axisVector = Vector3(0,0,0);
private var down : boolean = false;
private var mouseDownPosition;
private var mousePosition;
private var cubeOnDownPosition;
private var deltaMousePosition = Vector3(0,0,0);

function Update()
{
	if(down)
	{
		resize();
	}
}

//Is called from other script and gives the cubes target and what axis to follow
public function getTargetAndAxis (targetObject, axisToFollow)
{
	targetObjectForScaling = targetObject;
	axis = axisToFollow;
	axisVector[axis] = 1; 
}

//Enables resize() to be run from update() through a boolean + saves the cursor's and cube's position at mouse button down
function OnMouseDown()
{
	down = true;
	//raycastTool is used here as a ordinary raycast, 11 is the layer it ignores
	mouseDownPosition = raycastTool.getHitPoint(11);
	cubeOnDownPosition = transform.position;
}

function OnMouseUp()
{
	down = false;
	//TODO - Reset position of sizeCube
}

//Compares the distance the mouse has moved in the enabled axis and moves the cube the same distance in the same direction
function resize()
{	
	mousePosition = raycastTool.getHitPoint(11);
	if(mousePosition == false)
		return;
	else
	{
		//Calculates how far the cursor has moved
		deltaMousePosition = mousePosition - mouseDownPosition;
		
		//Converts the pos to localspace
		var spaceVector = transform.TransformDirection(axisVector); //Other solution?
		//var localDeltaMousePosition = ?;
		
		//Moves the sizeCube along the objects local axis
		//transform.Translate(localDeltaMousePosition);
	}
}

You will have to use matrix multiplications to replicate TransformPoint without the scale.

The alternative is that you have a second object that does the same but does not have the scale.