Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Camera.ScreenPointToRay hit Entites with Physics Bodies?

Discussion in 'Physics for ECS' started by MadboyJames, Oct 22, 2019.

  1. MadboyJames

    MadboyJames

    Joined:
    Oct 28, 2017
    Posts:
    262
    Hi, I have a tile grid comprised of entites with physics objects. When I use the Camera.ScreenPointToRay(Input.MousePosition), I get an error stemming from the ray not colliding with anything. I know how to cast rays on entities using Collisionworld.CastRay. I am unsure of how to get the end position for my raycast though. Would I use Camera.Forward*rayLength? How does the camera get its ray direction?

    Any help is appreciated.
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,732
    Camera.ScreenPointToRay(Input.MousePosition) gives the ray which has

    ray.origin
    ray.direction

    This lets you do

    startpoint = ray.origin;
    endpoint = ray.origin + ray.direction * distance;

    where distance is how far you want the ray to travel from the camera
     
  3. MadboyJames

    MadboyJames

    Joined:
    Oct 28, 2017
    Posts:
    262
    Okay, Thanks! I got it working. :)