Getting terrain pos at mouse pos

Hi,

I’m trying to get the terrain pos on the terrain where the mouse is at.
I have tried to search and havn’t found anything that works.

This code should do it but when i change to Scene mode to look at the line drawn it is completley off what it should be.

var mousePo = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x,  Input.mousePosition.y, Camera.main.nearClipPlane));

var ray = Camera.main.ScreenPointToRay(mousePo);

var hit:RaycastHit;
if(Physics.Raycast(ray.origin,ray.direction, hit))
{
     print("world point on terrain: " + hit.point + ", distance to point: " + hit.distance);
     Debug.DrawLine (ray.origin, hit.point);
}

So I think it has something to di with my camera code:

Can anyone see if the camera movment, rotation/pan and zoom code do’s anything wierd that messes up the code above?

“The target variable is just something so that you should see the camera rotation point.
You move the cmaera with a,s,d,w or arrows and hold shift+move mouse to pan the camera. Use scroll wheel to zoom.”

var target : Transform;
var distance = 350.0;

var xSpeed = 450.0;
var ySpeed = 220.0;

var yMinLimit = 0;
var yMaxLimit = 89;

private var x = 0.0;
private var y = 0.0;

var maxDist : float = 400;
var minDist : float = 75;
var zoomSpeed : float = 25;

@AddComponentMenu("Camera-Control/Mouse Orbit")
partial class MouseOrbit { }

function Start () 
{
    var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;

    // Make the rigid body not change rotation
    if (rigidbody)
        rigidbody.freezeRotation = true;
}

function LateUpdate () 
{
    if (target) 
	{
		var speed : float = 300.0;
		var modifier : float=1.0;
		
		// Mouse pan
		if(Input.GetKey (KeyCode.LeftShift))
		{
			x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
			y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
		}

		y = ClampAngle(y, yMinLimit, yMaxLimit);
		
		// Update target rotation
		target.rotation = Quaternion.Euler(0.0, x, 0.0) ;
		
		// Update camera target position
		// Move forward and backward 
		var forward = target.forward * modifier * Time.deltaTime * speed * Input.GetAxis("Vertical");
		target.position+=forward;
		
		// Strafe right and left
		var right = target.right * modifier * Time.deltaTime * speed * Input.GetAxis("Horizontal");
		target.position+=right;
		
		//var up = Vector3.up * modifier * Time.deltaTime * speed;
		//target.position+=up;
		
		// Zoom in and out
		if (Input.GetAxis("Mouse ScrollWheel") < 0  distance < maxDist)
		{
			distance += zoomSpeed;                             
			transform.Translate(Vector3.forward * -zoomSpeed); 
		}
		else if (Input.GetAxis("Mouse ScrollWheel") > 0  distance > minDist)
		{
			distance -= zoomSpeed;                             
			transform.Translate(Vector3.forward * zoomSpeed); 
		}	
		
		// Restrict the target y position to the height of the terrain at the current target position
		target.position.y  = 45;// Terrain.activeTerrain.SampleHeight(target.position);
	
		// Update camera position	
		var rotation = Quaternion.Euler(y, x, 0);
		var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;

		transform.rotation = rotation;
		transform.position = position;
		
		// Restrict the camera y position to the height of the terrain at the current camera position if the camera would be under the ground
		if(transform.position.y <= Terrain.activeTerrain.SampleHeight(transform.position) + 5)
		{
			transform.position.y  = Terrain.activeTerrain.SampleHeight(transform.position) + 5;
		}
    }
}

// Make sure the angle dosn't excede 0 - 360 degrees
static function ClampAngle (angle : float, min : float, max : float) 
{
    if (angle < -360)
        angle += 360;
    if (angle > 360)
        angle -= 360;
    return Mathf.Clamp (angle, min, max);
}

I really need to be able to get the terrain pos at the location I click with the mouse so that I can continue working, I’m kind of stuck here.

i only have it in c#

// prepare raycast
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 1000))
            {

}

hit will provide you the necessary properties which are listed here: Unity - Scripting API: RaycastHit

cheers

Thanks that worked perfect, not sure what I did wrong but now I can continue with some actual gameplay.

Here is the final javascript that I added to a sphere, the sphere will always be on the terrain where the mouse is:
(The sphere can’t have collision because that will make it move against the screen in a rather scarry way ;))

function Update () 
{
    // prepare raycast
    var hit:RaycastHit;
    var ray:Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, hit, 1000))
    {
          transform.position = hit.point;
    }
}