banner



How To Prevent Camera Clipping In Unity

Hullo I'm trying to do a basic Camera Collision in unity, for convenience, here's the unabridged script (I really got most of it from someone else who wrote the comments, if that'due south necessary to know):

///   ///   /// CameraControllercs.cs  /// Camera Controller in CSharp v2.1  ///   using UnityEngine; using System.Collections; using UnityEngine.EventSystems;  public form CameraController : MonoBehaviour {     public GameObject target;                           // Target to follow      public float targetHeight = ane.7f;                         // Vertical offset aligning      public float distance = 12.0f;                            // Default Distance      public float offsetFromWall = 0.7f;                       // Bring camera away from any colliding objects      public float maxDistance = 20f;                       // Maximum zoom Distance      public float minDistance = 0.6f;                      // Minimum zoom Distance      public bladder xSpeed = 200.0f;                             // Orbit speed (Left/Right)      public float ySpeed = 200.0f;                             // Orbit speed (Up/Down)      public float yMinLimit = -80f;                            // Looking upwards limit      public float yMaxLimit = 80f;                             // Looking down limit      public float zoomRate = 40f;                          // Zoom Speed      public float rotationDampening = iii.0f;                // Car Rotation speed (higher = faster)      public float zoomDampening = v.0f;                    // Auto Zoom speed (Higher = faster)      public int collisionLayers;     // What the camera will collide with      public bool lockToRearOfTarget = false;             // Lock photographic camera to rear of target      public bool allowMouseInputX = true;                // Allow histrion to control camera bending on the X axis (Left/Correct)      public bool allowMouseInputY = true;                // Allow player to control camera angle on the Y centrality (Up/Downwards)       individual float xDeg = 0.0f;     private float yDeg = 0.0f;     private bladder currentDistance;     private bladder desiredDistance;     private bladder correctedDistance;     private bool rotateBehind = simulated;     private bool mouseSideButton = false;     individual float pbuffer = 0.0f;       //Cooldownpuffer for SideButtons      individual float coolDown = 0.5f;      //Cooldowntime for SideButtons        GameObject primary;     void Starting time()     {         collisionLayers = LayerMask.GetMask("Default");         var temp = GameObject.Detect("chief");         if(temp != null)         {             master = temp;          }          Vector3 angles = transform.eulerAngles;         xDeg = angles.ten;         yDeg = angles.y;         currentDistance = distance;         desiredDistance = altitude;         correctedDistance = distance;         Camera.main.nearClipPlane = 0.001f;           if (lockToRearOfTarget)             rotateBehind = truthful;     }     void Update()     {         if (target == null)         {             target = GameObject.FindGameObjectWithTag("Actor") as GameObject;             Debug.Log("Looking for Histrion");         }      }      //Simply Movement camera after everything else has been updated      void FixedUpdate()     {         // Don't do anything if target is not divers          if (target == zip)             return;         //pushbuffer          if (pbuffer > 0)             pbuffer -= Time.deltaTime;         if (pbuffer  currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening * 3);          // Proceed within limits          currentDistance = Mathf.Clench(currentDistance, minDistance, maxDistance);          // Recalculate position based on the new currentDistance          position = target.transform.position - (rotation * Vector3.forrard * currentDistance + vTargetOffset);          //Finally Set rotation and position of camera          transform.rotation = rotation;         transform.position = position;      }      private void RotateBehindTarget()     {         float targetRotationAngle = target.transform.eulerAngles.y;         float currentRotationAngle = transform.eulerAngles.y;         xDeg = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Fourth dimension.deltaTime);          // Stop rotating behind if not completed          if (targetRotationAngle == currentRotationAngle)         {             if (!lockToRearOfTarget)                 rotateBehind = false;         }         else             rotateBehind = true;      }       private bladder ClampAngle(bladder angle, bladder min, float max)     {         if (angle  360f)             angle -= 360f;         return Mathf.Clamp(angle, min, max);     }  }

As you tin can encounter in the get-go function, I besides set the near clipping to a low value (the camera has a MainCamera tag BTW).

The problem: as yous can come across in this picture enter image description here

even with the low nearClipping value, and the high offsetFromWall value (0.7f when it'due south normally 0.1f), the camera is nonetheless clipping the footing when moving it with the mouse semi-fast (when it's moved kind of dull information technology's not a problem).

I have no idea how to fix this, if I make the nearClipping lower the whole render gets messed up, and even then it doesn't work, then what else can I do to ensure that the user never sees underneath the ground?

Source: https://gamedev.stackexchange.com/questions/162684/how-to-completely-stop-the-camera-from-clipping-into-the-ground

Posted by: maciassonififf.blogspot.com

0 Response to "How To Prevent Camera Clipping In Unity"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel