Moving objects with *Isometric* mouse position (JS)

Hello there!

My scene has an orthographic camera at 45 degrees on the x & y axis.

I have this piece of code placed to an object that follows the mouse around. The camera being 270 units off the ground meant to avoid placing the object directly under the camera I had to add 195 units to the z & x axis. There is also a clamp to ensure it doesn’t leave the ground. Eventually I want this to depend on terrain height, but i’m more concerned about the current situation of it not moving exactly to the mouse position. The Y axis must ALWAYS be at 0 as the item must not leave the ground. My scene is basically the beginnings of a system where you place a building on terrain (which has no hills, at the current time, so it doesn’t matter if the Y axis is stuck to 0).

My code works nicely for side to side mouse swiping, but upwards and downwards it moves very slowly compared to sideways. Is there a better way to do this so it ACTUALLY DOES follow the mouse, or is it always going to have an offset that I must work out how to counteract, and can you help me work out how to counteract this?

var myCamera : Camera;

function Update () {
	var vec : Vector3 = myCamera.ScreenToWorldPoint (Input.mousePosition);
	var top : float = 0.0;
	
	vec.y = Mathf.Clamp(vec.y, -top, top);

vec.z = vec.z + 195;
vec.x = vec.x + 195;
	
	//vec.z = 0.0;
	transform.position = vec;

}

Kind regards,
Liam

One solution is to use Unity’s mathematical Plane class. Since you always want the ‘y’ to be zero, you would place your plane at the origin (Vector3.zero), and use Vector3.up as the normal. Then you can use Camera.ScreenPointToRay() with the mouse position. And then use the ray in Plane.Raycast() to position your object.

I don’t have access to Unity to writeup some sample source, but this code has been posted before a few times.