RayCasting from screen centre point (cross hair)

Hi guys,
I want to do first person shooting mechanism.I want to raycast from my screen center point(exactly cross hair).But i don’t know how to get position of screen coordinates to start raycast. Even i tried to get camera target_vector.I am little bit confused to access camera attributes.How can i get target_vector of a camera
Thanks in advance

Regard’s

—Rajesh—

Hi Rajesh,

I’m a total Unity noob (I just downloaded it 2 days ago) but I believe this maybe what you are looking for:

function Update() {
    var x = Screen.width / 2;
    var y = Screen.height / 2;
    
    var ray = camera.ScreenPointToRay(Vector3(x, y, 0));
    Debug.DrawRay(ray.origin, ray.direction * 1000, Color.yellow);
}

or in C#:

    void Update()
    {
        int x = Screen.width / 2;
        int y = Screen.height / 2;

        Ray ray = camera.ScreenPointToRay(new Vector3(x, y));

        Debug.DrawRay(ray.origin, ray.direction * 1000, new Color(1f,0.922f,0.016f,1f));

    }

Hope it helps.

1 Like