move an object to where im clicking

Iv been bashing my head against google trying to figure this out but i just don’t understand.
the code i have so far, which i don’t really even understand is this:

function Update () 
{
	if(Input.GetMouseButton(1)) // if i click
	{
	    var ray = Camera.main.ScreenPointToRay(Input.mousePosition); //i think this shoots a ray from the camera to were the mouse is?
	    var selected : RaycastHit; 
	    if (Physics.Raycast(ray, selected)) //shoot "ray" and return the coords to "selected"???
	    {
			runPoint.position = selected.point;
	    }
	}

}

The links that i cannibalized these lines from looked like just what i needed but nothing is happening
i just want to put the transform “runpoint” where i clicked.
If anyone could tell me if this is close to what i need, or give me tip or anything ill be one happy camper.

You are probably interested in finding a point on terrain where the ray hit. If so, you need to have a reference to the terrain object, then get Collider component of that object and finally check if the ray actually hit your terrain.

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit rcHist;
GameObject terrain = GameObject.Find("yourTerrainName");
Collider collider= terrain.GetComponent<Collider>();

// 500 is a distance. Make sure to adjust it to your needs.
if(collider.Raycast(ray, out rcHist, 500))
{
    //here we know that the ray hit your terrain
    //RaycastHit object has all information you need. 
    //rcHist.point contains information where exactly the ray hit your terrain
}

If you can’t decrypt C# let me know, I just don’t feel comfortable with UnityScript.

yeah i think i can figure it out from there thanks!

  • actually on second thought do you think you could explain line 7 for me?
    that would help alot.