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 through a custom camera projection matrix

Discussion in 'Scripting' started by mstevenson, Oct 29, 2011.

Thread Status:
Not open for further replies.
  1. mstevenson

    mstevenson

    Joined:
    Sep 24, 2009
    Posts:
    189
    After modifying my main camera's projection matrix, all methods that use ray casting (OnMouseDown, ScreenPointToRay) begin to fail. From what I can tell, the ray continues to use the camera's original matrix.

    Is there any way to force a ray to use a custom projection matrix?
     
  2. shaderx

    shaderx

    Joined:
    May 9, 2010
    Posts:
    65
    you can use ur new matrix to transform the point to world space and use a regular ray test
     
  3. mstevenson

    mstevenson

    Joined:
    Sep 24, 2009
    Posts:
    189
    What is the method for this?
     
  4. shaderx

    shaderx

    Joined:
    May 9, 2010
    Posts:
    65
    you multiply the point by the inverse matrix of your projection matrix, but the viewport point not the screen point , the result will be a point in world space which means the same space for all other objects in the scene , then you can use Physics.Raycast or Physics.Linecast for testing
     
  5. mstevenson

    mstevenson

    Joined:
    Sep 24, 2009
    Posts:
    189
    Ah, I see, thanks! I need to brush up on my linear algebra.
     
  6. BugVito

    BugVito

    Joined:
    Sep 18, 2012
    Posts:
    1
    So if I understand correctly, the following should work:

    Code (csharp):
    1. Vector3 mousePosition = camera.ScreenToViewportPoint(Input.mousePosition);
    2. Ray ray = camera.ScreenPointToRay (camera.projectionMatrix.inverse.MultiplyPoint(mousePosition));
    3. bool objectsHit = Physics.Raycast (ray, out hit);
    4. Vector3 hitLocation = hit.point;
    Now this gives me funky results, so I messed up somewhere. Could anyone point out my mistake?
     
  7. DSimao

    DSimao

    Joined:
    Jun 11, 2014
    Posts:
    1
    Hi,
    Has anybody already solved this?
    I'm having same problems. I have a custom projection matrix on my camera and raycast doesn't work. Can anybody help me please?
     
  8. BigGameCompany

    BigGameCompany

    Joined:
    Sep 29, 2016
    Posts:
    107
    following - would love to see a solution for this.
     
  9. MattDavis

    MattDavis

    Joined:
    Aug 11, 2013
    Posts:
    16
    Code (CSharp):
    1.  
    2. public Ray ViewportPointToRay(Vector3 position)
    3.     {
    4.         position.x = (position.x - 0.5f) * 2f;
    5.         position.y = (position.y - 0.5f) * 2f;
    6.         position.z = -1f;
    7.  
    8.         var viewportToWorldMatrix = (_thisCamera.projectionMatrix * _thisCamera.worldToCameraMatrix).inverse;
    9.  
    10.         var rayOrigin = viewportToWorldMatrix.MultiplyPoint(position);
    11.  
    12.         var endPosition = position;
    13.         endPosition.z = 1f;
    14.         var rayEnd = viewportToWorldMatrix.MultiplyPoint(endPosition);
    15.  
    16.         return new Ray(rayOrigin, rayEnd - rayOrigin);
    17.     }
    18.  
     
    DragonCoder likes this.
Thread Status:
Not open for further replies.