Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to implement the code that was freely given by a very kind developer at the following link:

Right now, all I'm trying to do is bring it in on one camera, but I have a few issues. My class, looks as follows:

using UnityEngine;
using System.Collections;

public class PerspectiveOffCenter : MonoBehaviour 

    // Use this for initialization
    void Start () {


    // Update is called once per frame
    void Update () {


    public static Matrix4x4 GeneralizedPerspectiveProjection(Vector3 pa, Vector3 pb, Vector3 pc, Vector3 pe, float near, float far)
        Vector3 va, vb, vc;
        Vector3 vr, vu, vn;

        float left, right, bottom, top, eyedistance;

        Matrix4x4 transformMatrix;
        Matrix4x4 projectionM;
        Matrix4x4 eyeTranslateM;
        Matrix4x4 finalProjection;

        ///Calculate the orthonormal for the screen (the screen coordinate system
        vr = pb - pa;
        vu = pc - pa;
        vn = Vector3.Cross(vr, vu);

        //Calculate the vector from eye (pe) to screen corners (pa, pb, pc)
        va = pa-pe;
        vb = pb-pe;
        vc = pc-pe;

        //Get the distance;; from the eye to the screen plane
        eyedistance = -(Vector3.Dot(va, vn));

        //Get the varaibles for the off center projection
        left    = (Vector3.Dot(vr, va)*near)/eyedistance;
        right   = (Vector3.Dot(vr, vb)*near)/eyedistance;
        bottom  = (Vector3.Dot(vu, va)*near)/eyedistance;
        top     = (Vector3.Dot(vu, vc)*near)/eyedistance;

        //Get this projection
        projectionM = PerspectiveOffCenter(left, right, bottom, top, near, far);

        //Fill in the transform matrix
        transformMatrix = new Matrix4x4();

        transformMatrix[0, 0] = vr.x;
        transformMatrix[0, 1] = vr.y;
        transformMatrix[0, 2] = vr.z;
        transformMatrix[0, 3] = 0;

        transformMatrix[1, 0] = vu.x;
        transformMatrix[1, 1] = vu.y;
        transformMatrix[1, 2] = vu.z;
        transformMatrix[1, 3] = 0;

        transformMatrix[2, 0] = vn.x;
        transformMatrix[2, 1] = vn.y;
        transformMatrix[2, 2] = vn.z;
        transformMatrix[2, 3] = 0;

        transformMatrix[3, 0] = 0;
        transformMatrix[3, 1] = 0;
        transformMatrix[3, 2] = 0;
        transformMatrix[3, 3] = 1;

        //Now for the eye transform
        eyeTranslateM = new Matrix4x4();

        eyeTranslateM[0, 0] = 1;
        eyeTranslateM[0, 1] = 0;
        eyeTranslateM[0, 2] = 0;
        eyeTranslateM[0, 3] = -pe.x;

        eyeTranslateM[1, 0] = 0;
        eyeTranslateM[1, 1] = 1;
        eyeTranslateM[1, 2] = 0;
        eyeTranslateM[1, 3] = -pe.y;

        eyeTranslateM[2, 0] = 0;
        eyeTranslateM[2, 1] = 0;
        eyeTranslateM[2, 2] = 1;
        eyeTranslateM[2, 3] = -pe.z;

        eyeTranslateM[3, 0] = 0;
        eyeTranslateM[3, 1] = 0;
        eyeTranslateM[3, 2] = 0;
        eyeTranslateM[3, 3] = 1f;

        //Multiply all together
        finalProjection = new Matrix4x4();
        finalProjection = Matrix4x4.identity * projectionM*transformMatrix*eyeTranslateM;

        //finally return
        return finalProjection;

    // Update is called once per frame

    public void FixedUpdate () 
        Camera cam = camera;

        //calculate projection
        Matrix4x4 genProjection = GeneralizedPerspectiveProjection( new Vector3(0,1,0), new Vector3(1,1,0), new Vector3(0,0,0), new Vector3(0,0,0), cam.nearClipPlane, cam.farClipPlane);
                                                                //(BottomLeftCorner, BottomRightCorner, TopLeftCorner, trackerPosition, cam.nearClipPlane, cam.farClipPlane);
        cam.projectionMatrix = genProjection;   


My error lies in projectionM = PerspectiveOffCenter(left, right, bottom, top, near, far);

The debugger states:

Expression denotes a `type', where a 'variable', 'value' or 'method group' was expected.

Thus, I changed the line to read: projectionM = new PerspectiveOffCenter(left, right, bottom, top, near, far);

But then the error is changed to:

The type 'PerspectiveOffCenter' does not contain a constructor that takes '6' arguments.

For reasons that are obvious.

So, finally, I changed the line to read: projectionM = new GeneralizedPerspectiveProjection(left, right, bottom, top, near, far);

And the error I get is:

is a 'method' but a 'type' was expected.

With this last error, I'm not sure what it is I should do / missing. Can anyone see what it is that I'm missing to fix this error?

share|improve this question
up vote 0 down vote accepted

You're trying to complete the method GeneralizedPerspectiveProjection() I'm pretty sure trying to use GeneralizedPerspectiveProjection() within that method is a poor choice. I dodn't see anything to indicate that this was a recursive method.

Here's your problem:

public class PerspectiveOffCenter : MonoBehaviour

Your class is named PerspectiveOffCenter. Then when you're trying to run the code you pasted in you're getting the following error:

The type 'PerspectiveOffCenter' does not contain a constructor that takes '6' arguments.

From the code you've shown, it clearly doesn't contain a constructor that takes any arguments, let alone six. You said "For reasons that are obvious." But, I'm not sure they were obvious to you.

Your code is incomplete. You copied an pasted without checking that everything was included. You need to find or create a proper constructor for your class if you want to use that code. A quick Google search yielded this. But I don't know if that's the same code the original author used or not. You'll have to ask them, or figure out what's required based on your current code and write it yourself.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.