Why isn't ray-casting working?

the title says it all, unfortunately ray-casting isn’t working i cant see red line why? :frowning:

void Update () {
        RaycastHit hit;
        Ray CrosshairRay = new Ray(transform.position,Vector3.forward);

        Debug.DrawRay (transform.position, Vector3.forward * 100,Color.red);


        if (Physics.Raycast (CrosshairRay, out hit, PickUpDistance)) {
            if(hit.collider.tag == "Weapon"){
                Debug.Log("weapon found");
            }
        }
    }

This is my first ever time using ray-casting so i’m not good at it.

[update] It does work i just found out, but i’m casting a ray from middle crosshair, but since the image is calculated somewhere else but canvas is showed on game screen how do i cast it out from the player crosshair.

Sure you don’t want transform.forward, not Vector3.forward? One is relative to local space and the other is relative to world space.

i

It’s going relative to the canvases position which the image i want to create the ray from starts, but i want it to start from the image when in game mode on the player screen.

Can you post a screenshot? Having a hard time visualizing this.

I’m not sure that “Vector3.forward” works in the way that you think it does in these instances. Perhaps for finding the ray you should use transform.position and then transform.TransformPoint(Vector3.forward) instead for the direction. That’ll give the real-world coordinates of the direction that the current transform is facing, instead of 0,0,1 in world space?

I could be completely wrong, as I’m casting rays from the camera 99% of the time and haven’t used them in this way.

EDIT: Actually, I think @GroZZleR covered this already using an easier method, I just missed it. Sorry.

I moved the script from the image to the camera and it works now, thanks for your help.