ViewPortToWorldPoint problem

I have a crosshair that should shoot targets with raycasting. The raycasting part is OK, however I can´t make the transition between the screen point to the world point in order to fill the raycast method. My ViewPortToWorldPoint always return the same point regardless of where is the crosshair transform.

This is how it looks like:

public void Shoot()
{
    Vector3 currentShootPositionInViewPort = new Vector3(crosshair.position.x, crosshair.position.y, crosshair.position.z);

    print(currentShootPosition); //gives me changing values when I move the crosshair

    Vector3 worldPos = cam.camera.ViewportToWorldPoint(currentShootPosition);

    print(worldPos); //Always give me the same value???
}

If someone can point me something wrong I am doing… thanks!

By the way, the ScreenToWorldPoint also gives me the same value… I´ve tested that too. Possibly a problem with the crosshair transform?

The viewport is 2d, but the world is 3d. ViewportToWorldPoint combines x and y viewport coordinates with a distance z from the camera to generate a 3d point. For instance: ViewportToWorldPoint(Vector3(0.5f,0.5f,100f)) will trace an imaginary straight line from the center of the camera to a point 100m ahead, and return this point’s coordinates. That’s why you’re always having the same result: the z coordinate at the GUITexture position probably is 0, what always returns the camera position.

If you want to shoot at the crosshair position, you must create a ray using ViewportPointToRay:

public void Shoot()
{   // only x and y are considered; z is ignored by ViewportPointToRay
    Ray ray = camera.ViewportPointToRay(crosshair.position);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit)){
        // something was hit
    }
}

This function was kind of “stolen” from the ViewportPointToRay example.

I just made it a screen point before turning into a world point because I had to add the texture offset to center the aim both in X and Y. Then I turned it into a Ray with ScreenPointToRay method.

When I switch between DrawLine and DrawRay I see different things… Also, it does not collide with the huge cube just in front of camera, certainly being hit by the ray. Probably a mistake by my part?

    public void Shoot()
    {
        Vector3 scrp = Camera.main.ViewportToScreenPoint(this.transform.position);
        scrp = new Vector3(scrp.x + 24, scrp.y + 24, scrp.z);
        ray = Camera.main.ScreenPointToRay(scrp);
        //ray.direction = new Vector3(0, 0, -10);
        RaycastHit hit;
        if (Physics.Raycast(ray.origin, Vector3.forward, out hit, Mathf.Infinity))
        {
            print("hit!");
        }
        else print("not hit!");
        print(ray.ToString());

    }

    // Update is called once per frame
	void Update () 
    {
        Debug.DrawRay(ray.origin, ray.direction, Color.red);
        //Debug.DrawLine(ray.origin, ray.direction);
    }

It seems to be a bug in the current version of unity. However ViewportPointToRay works, so I can suggest you temporarily replace

cam.camera.ViewportToWorldPoint(currentShootPosition);
with
cam.camera.ViewportPointToRay(currentShootPosition).origin;