Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Raycasting with Lens Distortion

Discussion in 'Scripting' started by Uncleared, Nov 19, 2020.

  1. Uncleared

    Uncleared

    Joined:
    Jan 23, 2016
    Posts:
    8
    I'm creating a game that involves you raycasting on objects through a screen distorted by the lens distortion effect from post processing.
    However, because it is distorted, the raycast no longer works, because of the warped screen coordinates.
    I'm using URP.
    How would you guys suggest I fix this?
    This is what it looks like:
    upload_2020-11-19_14-58-44.png
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
  3. Uncleared

    Uncleared

    Joined:
    Jan 23, 2016
    Posts:
    8
    I have tried that, I've implemented the code and it doesn't work. I'm using the URP post processing so it may be different, how the lens distortion works
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Uncleared

    "I've implemented the code and it doesn't work."

    I haven't done anything like that before with shaders, but remember the clicks on screen item hit the distorted position, so shouldn't you remove the distortion, to get the actual object world or screen position to raycast against?

    Maybe add some debug draw lines or OnDrawGizmos to verify your click position and actual object (undistorted) positions match?
     
  5. Uncleared

    Uncleared

    Joined:
    Jan 23, 2016
    Posts:
    8
    im not sure what kind of algorithm it uses, but I might try to use gizmos to see how it distorts