Search Unity

RayCasting from screen centre point (cross hair)

Discussion in 'Scripting' started by Gamer_, Dec 24, 2009.

  1. Gamer_

    Gamer_

    Joined:
    Dec 4, 2009
    Posts:
    74
    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---
     
  2. damien_oconnell

    damien_oconnell

    Joined:
    Dec 24, 2009
    Posts:
    58
    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:


    Code (csharp):
    1. function Update() {
    2.     var x = Screen.width / 2;
    3.     var y = Screen.height / 2;
    4.    
    5.     var ray = camera.ScreenPointToRay(Vector3(x, y, 0));
    6.     Debug.DrawRay(ray.origin, ray.direction * 1000, Color.yellow);
    7. }
    or in C#:


    Code (csharp):
    1.     void Update()
    2.     {
    3.         int x = Screen.width / 2;
    4.         int y = Screen.height / 2;
    5.  
    6.         Ray ray = camera.ScreenPointToRay(new Vector3(x, y));
    7.  
    8.         Debug.DrawRay(ray.origin, ray.direction * 1000, new Color(1f,0.922f,0.016f,1f));
    9.  
    10.     }
    Hope it helps.
     
    ivan-acebes likes this.