Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

ScreenPointToRay

Discussion in 'Scripting' started by renman3000, Dec 21, 2011.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,700
    Hi there,
    I am just wondering, and if I am wrong ok, please tell me so but I am getting the perception that any line of code that is as ...

    ray = camera.ScreenPointToXXX()

    basically insructs that a ray be shot from the centerpoint of the camera out to the input position.


    If this is true it is not what I want. What I want is to have a ray shot directly from the point of input, out to the world in the direction of z.forward.

    Can some one fill me in on how to do this, if I am already doing this etc?
     
  2. Deleted User

    Deleted User

    Guest

    Sounds like you want to use Camera.ScreenToWorldPoint.

    But if you're trying to do something like object selection, what you're doing may be fine, e.g. that' what I use for tapping on objects in iOS, as in the code snippet at the bottom here:

    http://drupal.technicat.com/games/unity/iphone/input.html
     
    Last edited by a moderator: Dec 21, 2011
  3. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I'm not really sure what input position and point of input mean. This function finds you the object the mouse cursor is pointing to:
    Code (csharp):
    1. private GameObject GetSelectedObject()
    2. {
    3.     var ray = guiCamera.ScreenPointToRay(Input.mousePosition);
    4.     RaycastHit hit;
    5.     if (Physics.Raycast(ray, out hit))
    6.     {
    7.         return hit.collider.gameObject;
    8.     }
    9.  
    10.     return null;
    11. }
    12.  
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,700
    Ok, great I got it working to a point where I think I can "take it from here".


    Code (csharp):
    1.  
    2.  
    3. function Update ()
    4. {
    5.     if (Input.touchCount == 1)
    6.     {
    7.         var theTouch : Touch = Input.GetTouch(0);
    8.         var ray = Camera.main.ScreenPointToRay(theTouch.position);
    9.         var hit : RaycastHit;
    10.         if (Physics.Raycast(ray, hit))
    11.         {
    12.             print("oh yah");
    13.             print("ray" + ray);
    14.             print("hit.collider.gameObject.name" + hit.collider.gameObject.name);
    15.         }
    16.     }      
    17. }
    18.  

    Thanks.
     
  5. Screenhog

    Screenhog

    Joined:
    Jul 2, 2009
    Posts:
    498
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,700
    What do you mean it is dectecting it when it shouldnt be?
    What does shouldnt be mean?
     
  7. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I met it too, and am still trying to figure out why.
     
  8. Screenhog

    Screenhog

    Joined:
    Jul 2, 2009
    Posts:
    498
    When you fly around the scene that I uploaded, try to mouse over the capsule character in the center. The more you fly around, the more it seems to be detecting the capsule in places where there is nothing there. I can't seem to describe it better than that... I don't know why it's happening.
     
  9. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,700
    So what is it that is detecting the capsule? An update, ray dependant on mousePosition?


    Are your speeds incredibly fast?
     
  10. Screenhog

    Screenhog

    Joined:
    Jul 2, 2009
    Posts:
    498
    I send a ray with ScreenPointToRay from the camera. So yes, it's dependent on mousePosition. It shoots forward, and stops when it hits a collider... or at least it should.
     
  11. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,700
    and in now way should it be hitting your desired object? Maybe you have created a loop somewhere where, once the capsule is hit once, a loop is continued, endlessly despite no longer being struck.


    .?
     
  12. Screenhog

    Screenhog

    Joined:
    Jul 2, 2009
    Posts:
    498
    No, it should be hitting the desired object. The problem is that it believes it hits the object sometimes when the object isn't actually there.
     
  13. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,700
    Hmm. Totally not sure. I have never seen that. Mind i am very new to raycasting. Does not make sense tho. You sure your object is out of the way?
     
  14. Screenhog

    Screenhog

    Joined:
    Jul 2, 2009
    Posts:
    498
    Absolutely. I've double-checked the collider, and it's in the same place as the mesh. I've used Debug.Log to make sure of what object I'm hitting, and it's the capsule.