Anonymous Anonymous - 4 months ago
265 0

No description

C#

Unity Object screen space forward direction

{   // DEBUG. Project vehicle to normalized device space - then normalized
            // TODO: We have to compute and convert the swipes relative to the player vehicle's direction
            var currentCam = FindObjectOfType<CameraVectorFieldCamera>().GetComponent<Camera>();
            var toCameraMatrix = currentCam.worldToCameraMatrix;
            var projectionMatrix = currentCam.projectionMatrix;

            // Transform.position needs to be at the center point of camera view...
            var centerViewPos = currentCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, Vector3.Magnitude(transform.position - currentCam.transform.position)));
            Vector4 playerForward = centerViewPos + (transform.forward * 5.0f);
            playerForward.w = 1.0f;

            Vector4 playerForwadProjected = projectionMatrix * toCameraMatrix * playerForward;
            Vector4 playerForwadProjectedPersDiv = playerForwadProjected;

            playerForwadProjectedPersDiv.x /= playerForwadProjectedPersDiv.w;
            playerForwadProjectedPersDiv.y /= playerForwadProjectedPersDiv.w;
            playerForwadProjectedPersDiv.z /= playerForwadProjectedPersDiv.w;
            playerForwadProjectedPersDiv.w /= playerForwadProjectedPersDiv.w;

            Debug.Log("playerForwadProjected: " + playerForwadProjected + "\twithPersDiv: " + playerForwadProjectedPersDiv);
            Debug.Log("normalized: " + new Vector2(playerForwadProjectedPersDiv.x, playerForwadProjectedPersDiv.y).normalized);
        }