- Home /
Distance from raycast.
I don't see the problem here.
#pragma strict
var Block:GameObject;
var hit:RaycastHit;
function Start () {
Screen.lockCursor = true;
}
function Update () {
var distance = hit.distance;
if (Input.GetButtonDown ("Fire1")) {
var fwd = transform.TransformDirection (Vector3.forward);
var mousePos = Input.mousePosition;
//mousePos.z = Physics.Raycast (transform.position, fwd, hit.distance);
//mousePos.z = 3;
mousePos.z = distance;
var objectPos = Camera.main.ScreenToWorldPoint(mousePos);
Instantiate(Block, objectPos, Quaternion.identity);
}
}
It instantiates it at the camera
Comment
Answer by robertbu · Oct 16, 2014 at 10:14 PM
Unless you are doing it in the Inspector, 'hit' is never initialized to anything, so hit.distance will be 0. So when you do this:
mousePos.z = distance;
...is setting the 'z' distance to 0.0. If the 'z' value used in the Vector3 passed to ScreenToWorldPoint() is the distance in front of the camera. If 0.0 is passed, the returned Vector3 will be the camera position.