ViewportPointToRay() public method

Returns a ray going from camera through a viewport point.

public ViewportPointToRay ( Vector3 position ) : Ray
position Vector3
return Ray
コード例 #1
0
ファイル: ClampCamera.cs プロジェクト: Nachtrind/Fungus
 public void ClampCamViewPos(Camera cam)
 {
     cam.transform.position += GetClampedDelta(GetPoint(cam.ViewportPointToRay(Vector3.zero)));
     cam.transform.position += GetClampedDelta(GetPoint(cam.ViewportPointToRay(Vector2.one)));
     cam.transform.position += GetClampedDelta(GetPoint(cam.ViewportPointToRay(new Vector2(1, 0))));
     cam.transform.position += GetClampedDelta(GetPoint(cam.ViewportPointToRay(new Vector2(0, 1))));
 }
コード例 #2
0
        public static Vector3?ViewportPointToGround(this UnityEngine.Camera camera, Vector2 viewport)
        {
            var   ray    = camera.ViewportPointToRay(viewport);
            Plane hPlane = new Plane(Vector3.up, Vector3.zero);
            float distance;

            if (hPlane.Raycast(ray, out distance))
            {
                return(ray.GetPoint(distance));
            }
            return(null);
        }
コード例 #3
0
        public void MeleeAttack()
        {
            _lastShot = Time.fixedTime;

            var        ray = _viewCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit) && Vector3.Distance(transform.position, hit.point) < .5f)
            {
                if (hit.collider.tag == "Enemy")
                {
                    var hitContext = new HitContext
                    {
                        Damage    = Random.Range(0, 2),
                        Direction = transform.forward,
                        Force     = 1,
                        IsMelee   = true
                    };
                    hit.collider.GetComponent <HealthBehavior>().TakeDamage(hitContext);
                }
            }
        }
コード例 #4
0
 static public int ViewportPointToRay(IntPtr l)
 {
     try{
         UnityEngine.Camera  self = (UnityEngine.Camera)checkSelf(l);
         UnityEngine.Vector3 a1;
         checkType(l, 2, out a1);
         UnityEngine.Ray ret = self.ViewportPointToRay(a1);
         pushValue(l, ret);
         return(1);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
コード例 #5
0
 static public int ViewportPointToRay(IntPtr l)
 {
     try {
         UnityEngine.Camera  self = (UnityEngine.Camera)checkSelf(l);
         UnityEngine.Vector3 a1;
         checkType(l, 2, out a1);
         var ret = self.ViewportPointToRay(a1);
         pushValue(l, true);
         pushValue(l, ret);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
コード例 #6
0
    bool Raycast( Camera cam, Vector2 screenPos, out RaycastHit hit )
    {
        //Ray ray = cam.ScreenPointToRay( screenPos );
		Ray ray = cam.ViewportPointToRay( new Vector2( screenPos.x / Screen.width, screenPos.y / Screen.height ) );
        bool didHit = false;

        if( RayThickness > 0 )
            didHit = Physics.SphereCast( ray, 0.5f * RayThickness, out hit, Mathf.Infinity, ~IgnoreLayerMask );
        else
            didHit = Physics.Raycast( ray, out hit, Mathf.Infinity, ~IgnoreLayerMask );

        // vizualise ray
    #if UNITY_EDITOR
        if( VisualizeRaycasts )
        {
            if( didHit )
                Debug.DrawLine( ray.origin, hit.point, Color.green, 0.5f );
            else
                Debug.DrawLine( ray.origin, ray.origin + ray.direction * 9999.0f, Color.red, 0.5f );
        }
    #endif

        return didHit;
    }
コード例 #7
0
ファイル: SunshineMath.cs プロジェクト: GameDiffs/TheForest
 public static float RadialClipCornerRatio(Camera cam)
 {
     Ray ray = cam.ViewportPointToRay(new Vector3(0f, 0f, 0f));
     return cam.transform.InverseTransformDirection(ray.direction).z;
 }
コード例 #8
0
    /// <summary>
    /// Calculates the ratio between far clip at the corner of the view frustum, and at the center, assuming a radial clipping plane.
    /// </summary>
    /// <returns>
    /// The corner far clip ratio
    /// </returns>
    /// <param name='cam'>
    /// A camera.
    /// </param>
    public static float RadialClipCornerRatio(Camera cam)
    {
        //*
        var cornerRay = cam.ViewportPointToRay(new Vector3(0f, 0f, 0f));
        var localForward = cam.transform.InverseTransformDirection(cornerRay.direction);
        return localForward.z;
        //*/

        //The following should be better, since it's perfectly stable!
        //But... the math just isn't cooperating! >:/
        /*
        float vFOV = cam.fieldOfView;
        float radVFOV = vFOV * Mathf.Deg2Rad;
        float radHFOV = 2f * Mathf.Atan(Mathf.Tan(radVFOV / 2f) * cam.aspect);
        float hFOV = Mathf.Rad2Deg * radHFOV;
        return (Quaternion.Euler(vFOV * 0.5f, hFOV * -0.5f, 0f) * Vector3.forward).z;
        //*/
    }
コード例 #9
0
    /// <summary>
    /// Gets a Mesh that covers the plane where the viewing frustum
    /// of the playerView camera intersects the provided terrain
    /// 
    /// Use this to construct minimaps that show where
    /// the player is viewing
    /// </summary>
    /// <param name="terrain">the terrain the player is looking at</param>
    /// <param name="playerView">the camera the player is looking through</param>
    /// <returns></returns>
    public Mesh getViewBoxMesh(Terrain terrain, Camera playerView)
    {
        Ray[] rays = new Ray[4];
        float[] hits = new float[4];
        Vector3[] points = new Vector3[4];
        Vector3[] viewPortPoints = new Vector3[4];
        viewPortPoints[0] = new Vector3(0, 0, 0);
        viewPortPoints[1] = new Vector3(1, 0, 0);
        viewPortPoints[2] = new Vector3(1, 1, 0);
        viewPortPoints[3] = new Vector3(0, 1, 0);

        int[] tris = { 0, 3, 2, 2, 1, 0 };

        for (int i = 0; i < rays.Length; i++)
        {
            rays[i] = playerView.ViewportPointToRay(viewPortPoints[i]);
            Plane p = new Plane(terrain.transform.up, terrain.transform.position);
            p.Raycast(rays[i], out hits[i]);
            if (hits[i] < 0)
            {
                float pHit;

                Ray downRay = new Ray(rays[i].GetPoint(2 * getDiagSize(terrain.collider.bounds.size)), playerView.transform.up * -1);
                p.Raycast(downRay, out pHit);
                points[i] = downRay.GetPoint(pHit);
            } else {
                points[i] = rays[i].GetPoint(hits[i]);
            }
        }

        Mesh box = new Mesh();
        box.vertices = points;
        box.triangles = tris;
        box.uv = new Vector2[4];
        box.RecalculateNormals();
        return box;
    }
コード例 #10
0
ファイル: GameUnit.cs プロジェクト: tommai78101/Multiplier
 public void CastRay(bool isMinimap, Vector3 mousePosition, Camera minimapCamera)
 {
     if (isMinimap) {
         Ray ray = minimapCamera.ViewportPointToRay(mousePosition);
         RaycastHit[] hits = Physics.RaycastAll(ray, 1000f);
         foreach (RaycastHit hit in hits) {
             if (hit.collider.gameObject.tag.Equals("Floor")) {
                 CmdSetTarget(this.gameObject, hit.point);
                 break;
             }
         }
     }
     else {
         Ray ray = Camera.main.ScreenPointToRay(mousePosition);
         RaycastHit[] hits = Physics.RaycastAll(ray);
         foreach (RaycastHit hit in hits) {
             if (hit.collider.gameObject.tag.Equals("Floor")) {
                 //Call on the client->server method to start the action.
                 CmdSetTarget(this.gameObject, hit.point);
                 break;
             }
         }
     }
 }