Question by
J3nka · Aug 03, 2016 at 08:29 PM ·
scripting problemraycasthit.point
hit.point returns wrong coordinates
Hi! Im trying to spawn a prefab when I look down on the ground and are pressing "1". I want to spawn it at the hit point of a raycast but It's always returning the position (-2, 0, -19) in the world independently of where the camera is in the world.
Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.DrawRay(cam.transform.position, cam.transform.forward, Color.green);
if (hit.transform.name == "Ground" && hit.distance <= distance)
{
print("I'm looking at " + hit.transform.name + " and it's in range.");
if (Input.GetKey(KeyCode.Alpha1))
{
SpawnPoint = hit.point;
Instantiate(objectToBuild, SpawnPoint, Quaternion.identity);
}
}
}
I can tell that the raycast is working by checking the prints I got in the code so if you know an answer to this please let me now.
Comment