Problem with my math...

Hi!

I’m trying to set my camera to a point just in between my mousecursor and my gameobject but can’t seem to get the math… :frowning:

How can i get that point?
So far Ive just got the input.mouseposition and the gameobject.position but where do i go from here?
I know I can get the distance between these points and the angle, but how do I get the exact positionoffset from the gameobject?

Thanks
André

First you need to transform the pixel coordinates of the cursor into world coordinates with ScreenToWorldPoint(). Then use Vector3.Lerp() to get the midpoint.

var target : Transform;

function Update() {
	var cursorPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
	transform.position = Vector3.Lerp(cursorPosition, target.position, 0.5);
}

You might have to modify cursorPosition before moving the camera, because screen coordinates do not include depth. How you do this will depend on where you want your camera and what direction it is pointing in.

This is going to end up as a feed back loop that will jitter a lot because moving the camera moves the mouse.

You have a problem here which is that Input.mousePosition is in screen pixel coordinates and transform.position is in world coordinates.

First lets transfer mouse pixel coordinates into world coordinates.

// method one: Raycast to a surface. This will place marker on the surface the mouse is pointing to (requires collider).

var myGameObject : GameObject;
var marker : Transform;


private var hit : RaycastHit;

function Update ()
{
	ray = Camera.main.ScreenPointToRay(Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
	if(Physics.Raycast(ray, hit))
	{
		marker.position = hit.point;	
	}
	else // method two: move marker to a world position inside the camera frustum where 
		 // 0,0,0 is at the bottom left of the frustum on the near clip plane and
		 // 1 * Screen.width, 1 * Screen.width, 1 is at the top right of the frustum on the far clip plane
	{
		marker.position = Camera.main.ViewportToWorldPoint(Vector3(Input.mousePosition.x / Screen.width, Input.mousePosition.y / Screen.height, 0));
	}
	
	// now we can get the average position in between marker (mouse position in world space) and myGameObject
	
	average = (myGameObject.transform.position + marker.position) / 2;
	
	//We Use Lerp to avoid jitter
	Camera.main.transform.rotation = Quaternion.Lerp(Camera.main.transform.rotation, Quaternion.LookRotation(average - Camera.main.transform.position), Time.deltaTime * 3);
}

Good point. I’ve been working with my own cursor implementation for a while, so I’m not thinking in the right terms.

Thanks for the replies!

I’m sorry, but i should have told you that this will be a “2.5D” sidescroller where the mousepointer is the aim for the gun…
So I cannot use the “Ray way” of finding the surface because one will very seldom actually hit a surface.

Then you need to do a ScreenToWorldPoint. Feed it a vector3 which has the x mouse position, y mouse position a value which I think should be Mathf.InverseLerp(Camera.main.nearClipPlane, Camera.main.farClipPlane, distanceFromCameraToPlayer);

Got this to work at last!
Might as well share it :slight_smile:

Dunno if it might be better to store the Screen.width/2 (and height) as private var just to make it a little bit faster…

But it seems fast now too :stuck_out_tongue:

var target : Transform;
var scale = 15;
var distance = 50;
var smooth = 5;

@AddComponentMenu("Camera-Control/Smooth Look At")
partial class SmoothLookAt { }

function Awake ()
{
	transform.position.x = target.position.x;
	transform.position.y = target.position.y;
	
	transform.LookAt(target);
}

function Update () 
{
	// Get mouse pos to LowerLeft = -512,-385 UpperRight = +512,+384
	var mousePos = Input.mousePosition;
	mousePos.x = mousePos.x - Screen.width/2;
	mousePos.y = mousePos.y - Screen.height/2;
	
	// Scale it so the camera moves the correct distance
	var offset = mousePos / scale;
	
	// Set the offset and place the camera at "distance" from the target
	var newPos = target.position + offset;
	newPos.z = target.position.z - distance;
	
	// Make it silky smooth
	transform.position = Vector3.Lerp (
		transform.position, newPos, 
		Time.deltaTime * smooth);
}

Ah, forgot!
Stick this to your camera :stuck_out_tongue: