Search Unity

ProtectCameraFromWallClip.cs InvalidCastException: Cannot cast from source type to ....

Discussion in 'Scripting' started by 2pa, Nov 18, 2018.

  1. 2pa

    2pa

    Joined:
    Nov 8, 2013
    Posts:
    5


    Help, me please, how to fix it?

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4. namespace UnityStandardAssets.Cameras
    5. {
    6.     public class ProtectCameraFromWallClip : MonoBehaviour
    7.     {
    8.         public float clipMoveTime = 0.05f;              // time taken to move when avoiding cliping (low value = fast, which it should be)
    9.         public float returnTime = 0.4f;                 // time taken to move back towards desired position, when not clipping (typically should be a higher value than clipMoveTime)
    10.         public float sphereCastRadius = 0.1f;           // the radius of the sphere used to test for object between camera and target
    11.         public bool visualiseInEditor;                  // toggle for visualising the algorithm through lines for the raycast in the editor
    12.         public float closestDistance = 0.5f;            // the closest distance the camera can be from the target
    13.         public bool protecting { get; private set; }    // used for determining if there is an object between the target and the camera
    14.         public string dontClipTag = "Player";           // don't clip against objects with this tag (useful for not clipping against the targeted object)
    15.         private Transform m_Cam;                  // the transform of the camera
    16.         private Transform m_Pivot;                // the point at which the camera pivots around
    17.         public float m_OriginalDist;             // the original distance to the camera before any modification are made
    18.         private float m_MoveVelocity;             // the velocity at which the camera moved
    19.         public float m_CurrentDist;              // the current distance from the camera to the target
    20.         private Ray m_Ray = new Ray();                        // the ray used in the lateupdate for casting between the camera and the target
    21.         private RaycastHit[] m_Hits;              // the hits between the camera and the target
    22.         private RayHitComparer m_RayHitComparer;  // variable to compare raycast hit distances
    23.         public float scrollSpeed;
    24.         private void Start()
    25.         {
    26.             // find the camera in the object hierarchy
    27.             m_Cam = GetComponentInChildren<Camera>().transform;
    28.             m_Pivot = m_Cam.parent;
    29.             m_OriginalDist = 15f;
    30.             m_CurrentDist = m_OriginalDist;
    31.             // create a new RayHitComparer
    32.             m_RayHitComparer = new RayHitComparer();
    33.         }
    34.         private void LateUpdate()
    35.         {
    36.             m_OriginalDist -= Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
    37.             m_OriginalDist = Mathf.Clamp(m_OriginalDist, closestDistance, 25f);
    38.             // initially set the target distance
    39.             float targetDist = m_OriginalDist;
    40.             m_Ray.origin = m_Pivot.position + m_Pivot.forward*sphereCastRadius;
    41.             m_Ray.direction = -m_Pivot.forward;
    42.             // initial check to see if start of spherecast intersects anything
    43.             var cols = Physics.OverlapSphere(m_Ray.origin, sphereCastRadius);
    44.             bool initialIntersect = false;
    45.             bool hitSomething = false;
    46.             // loop through all the collisions to check if something we care about
    47.             for (int i = 0; i < cols.Length; i++)
    48.             {
    49.                 if ((!cols[i].isTrigger) &&
    50.                     !(cols[i].attachedRigidbody != null && cols[i].attachedRigidbody.CompareTag(dontClipTag)))
    51.                 {
    52.                     initialIntersect = true;
    53.                     break;
    54.                 }
    55.             }
    56.             // if there is a collision
    57.             if (initialIntersect)
    58.             {
    59.                 m_Ray.origin += m_Pivot.forward*sphereCastRadius;
    60.                 // do a raycast and gather all the intersections
    61.                 m_Hits = Physics.RaycastAll(m_Ray, m_OriginalDist - sphereCastRadius);
    62.             }
    63.             else
    64.             {
    65.                 // if there was no collision do a sphere cast to see if there were any other collisions
    66.                 m_Hits = Physics.SphereCastAll(m_Ray, sphereCastRadius, m_OriginalDist + sphereCastRadius);
    67.             }
    68.             // sort the collisions by distance
    69.             Array.Sort(m_Hits, m_RayHitComparer);
    70.             // set the variable used for storing the closest to be as far as possible
    71.             float nearest = Mathf.Infinity;
    72.             // loop through all the collisions
    73.             for (int i = 0; i < m_Hits.Length; i++)
    74.             {
    75.                 // only deal with the collision if it was closer than the previous one, not a trigger, and not attached to a rigidbody tagged with the dontClipTag
    76.                 if (m_Hits[i].distance < nearest && (!m_Hits[i].collider.isTrigger) &&
    77.                     !(m_Hits[i].collider.attachedRigidbody != null &&
    78.                       m_Hits[i].collider.attachedRigidbody.CompareTag(dontClipTag)))
    79.                 {
    80.                     // change the nearest collision to latest
    81.                     nearest = m_Hits[i].distance;
    82.                     targetDist = -m_Pivot.InverseTransformPoint(m_Hits[i].point).z;
    83.                     hitSomething = true;
    84.                 }
    85.             }
    86.             // visualise the cam clip effect in the editor
    87.             if (hitSomething)
    88.             {
    89.                 Debug.DrawRay(m_Ray.origin, -m_Pivot.forward*(targetDist + sphereCastRadius), Color.red);
    90.             }
    91.             // hit something so move the camera to a better position
    92.             protecting = hitSomething;
    93.          //   Debug.Log(targetDist);
    94.             m_CurrentDist = Mathf.SmoothDamp(m_CurrentDist, targetDist, ref m_MoveVelocity,
    95.                                            m_CurrentDist > targetDist ? clipMoveTime : returnTime);
    96.             m_CurrentDist = Mathf.Clamp(m_CurrentDist, closestDistance, 25f);
    97.             m_Cam.localPosition = -Vector3.forward*m_CurrentDist;
    98.         }
    99.         // comparer for check distances in ray cast hits
    100.         public class RayHitComparer : IComparer
    101.         {
    102.             public int Compare(object x, object y)
    103.             {
    104.                 return ((RaycastHit) x).distance.CompareTo(((RaycastHit) y).distance);
    105.             }
    106.         }
    107.     }
    108. }