Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Camera glitches into wall

Discussion in 'Scripting' started by grimmreefer, Nov 15, 2015.

  1. grimmreefer

    grimmreefer

    Joined:
    Jan 15, 2015
    Posts:
    31
    Hey together, i need help. To prevent that my camera moves into a mesh i use physics.linecast. Basically it works, but as you can see in the attached video the linecast glitches into the collider if i move or stop in the right position/camera angle.

    Any idea how i can solve this?

    Code (CSharp):
    1.            
    2. if (Physics.Linecast(new Vector3(transform.position.x, transform.position.y, transform.position.z),                                                           camHitPoint.transform.position, out hit))
    3.             {
    4.                 camHitPoint.transform.position = Vector3.Lerp(camHitPoint.transform.position, hit.point, (15 *                                                                                                           Time.deltaTime));
    5.             }
    6.             else
    7.             {
    8.                 camHitPoint.transform.localPosition = Vector3.Lerp(camHitPoint.transform.localPosition, camStartPos,                                                                                                   (10*Time.deltaTime));
    9.             }

     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    this is what i use to prevent camera clipping:
    Code (CSharp):
    1. Ray ray = new Ray(target.position, transform.position-target.position);
    2.         RaycastHit hit;
    3.         Debug.DrawRay(ray.origin, ray.direction*distance);
    4.         LayerMask mask = LayerMask.NameToLayer("Level");
    5.         if(Physics.Raycast(ray, out hit, preferedDistance, 1<<mask.value))
    6.         {
    7.                     float currentDistance = Vector3.Distance(hit.point, target.position);
    8.                 if(currentDistance < preferedDistance)
    9.                     distance = currentDistance - 0.1f;
    10.  
    11.         }
    12.         else
    13.             distance = preferedDistance;
    14.            
    15.        
    16.         if(distance <= 0)
    17.             distance = 0.1f;
     
  3. grimmreefer

    grimmreefer

    Joined:
    Jan 15, 2015
    Posts:
    31
    First, thank you. :)

    Which layer do you ignore? And were do you set the cam position?
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    it only casts again everything in de layer "Level".
    it's for a 3rd controller, so the camera is set by a mouseorbit script around the player.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseOrbit : MonoBehaviour
    5. {
    6.  
    7.     public Transform target;
    8.     public static float distance = 2.0f;
    9.     private float preferedDistance;
    10.  
    11.     public float xSpeed = 250.0f;
    12.     public float ySpeed = 120.0f;
    13.  
    14.     public int yMinLimit = -20;
    15.     public int yMaxLimit = 80;
    16.  
    17.     private float x = 0.0f;
    18.     private float y = 0.0f;
    19.  
    20.     void Start ()
    21.     {
    22.         preferedDistance = distance;
    23.         Vector3 angles = transform.eulerAngles;
    24.         x = angles.y;
    25.         y = angles.x;
    26.  
    27.            if (rigidbody)
    28.             rigidbody.freezeRotation = true;
    29.     }
    30.  
    31.     void LateUpdate ()
    32.     {
    33.         if(target)
    34.         {
    35.             x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
    36.             y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
    37.            
    38.              y = ClampAngle(y, yMinLimit, yMaxLimit);
    39.                  
    40.             Quaternion rotation = Quaternion.Euler(y, x, 0);
    41.             Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
    42.          
    43.             transform.rotation = rotation;
    44.             transform.position = position;
    45.         }
    46.         Ray ray = new Ray(target.position, transform.position-target.position);
    47.         RaycastHit hit;
    48.         Debug.DrawRay(ray.origin, ray.direction*distance);
    49.         LayerMask mask = LayerMask.NameToLayer("Level");
    50.         if(Physics.Raycast(ray, out hit, preferedDistance, 1<<mask.value))
    51.         {
    52.  
    53.                 float currentDistance = Vector3.Distance(hit.point, target.position);
    54.                 if(currentDistance < preferedDistance)
    55.                     distance = currentDistance - 0.1f;
    56.         }
    57.         else
    58.             distance = preferedDistance;
    59.          
    60.      
    61.         if(distance <= 0)
    62.             distance = 0.1f;
    63.     }
    64.  
    65.     static float ClampAngle (float angle, float min, float max)
    66.     {
    67.         if (angle < -360)
    68.             angle += 360;
    69.         if (angle > 360)
    70.             angle -= 360;
    71.         return Mathf.Clamp (angle, min, max);
    72.     }
    73. }
    74.  
     
  5. grimmreefer

    grimmreefer

    Joined:
    Jan 15, 2015
    Posts:
    31
    So, took me a while to figure it out but now it works very smooth, thank you very much my friend!
     
  6. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    no problem, glad it works for you.