Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Draw debug ray in perspective mode

Discussion in 'Editor & General Support' started by boby-bobs, May 28, 2015.

  1. boby-bobs

    boby-bobs

    Joined:
    Oct 18, 2013
    Posts:
    53
    I've had success casting and drawing debug rays with an Orthographic camera, but I change this to Perspective and none of my rays seem to work anymore (in the scene view, my debug ray is not moving with my mouse).

    This was my code for Orthographic, what do I need to do differently for perspective?


    public class Cursor : MonoBehaviour {

    // 45 degree angle down, same as camera angle
    private Vector3 gameAngle = new Vector3(0, -45, -45);
    public RaycastHit hit;
    public Ray ray;

    void Update() {
    // Rays
    ray = new Ray(Camera.main.ScreenToWorldPoint(Input.mousePosition), gameAngle);
    if (Debug.isDebugBuild)
    Debug.DrawRay(Camera.main.ScreenToWorldPoint(Input.mousePosition), gameAngle * 20, Color.green);
    }
    }
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    With an orthographic camera, you have an unnatural view of your objects where the verticies on your objects are not stretched or skewed in any way, they only change based on your camera position, rotation, and scale. With a perspective camera, you have a sort-of "fish eye view", where the verticies stretch apart the farther they are away from you (this emulates human vision). Your ray logic will not get you the ray you want for a perspective camera.

    Try using Camera.ScreenPointToRay() instead of Camera.ScreenToWorldPoint() to make your ray
     
  3. boby-bobs

    boby-bobs

    Joined:
    Oct 18, 2013
    Posts:
    53