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. Dismiss Notice

Camera Collision Doesn't Work

Discussion in 'Scripting' started by Trekko, Aug 23, 2020.

  1. Trekko

    Trekko

    Joined:
    Jan 24, 2018
    Posts:
    21
    Hi,
    For a long time I'm trying to make third person camera in Unity. After copy-pasting some scripts from the internet it was almost done, the camera worked and didn't colliding with the objects but later i found better camera script and camera collision suddenly stopped work. I tried to solve it by myself but after all night trying I decided to ask someone more experienced.
    Camera Parent Script:

    Code (CSharp):
    1. public class MyMovingCamera : MonoBehaviour
    2. {
    3.     [Header("My Vars")]
    4.     public Transform player;
    5.     public float smoothness = 0.125f;
    6.     public Vector3 offset;
    7.     public float mouseRotation;
    8.     Vector2 rotation = Vector2.zero;
    9.  
    10.     void Start()
    11.     {
    12.         Cursor.lockState = CursorLockMode.Locked;
    13.         Cursor.visible = false;
    14.         rotation.y = player.transform.eulerAngles.y;
    15.     }
    16.  
    17.     void LateUpdate()
    18.     {
    19.         rotation.y += Input.GetAxis("Mouse X") * mouseRotation;
    20.         offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * mouseRotation, Vector3.up) * offset;
    21.      
    22.         Vector3 destinyPosition = player.position + offset;
    23.         Vector3 smoothPosition = Vector3.Lerp(transform.position, destinyPosition, smoothness);
    24.         transform.position = smoothPosition;
    25.         transform.LookAt(player);
    26.         player.transform.eulerAngles = new Vector2(0, rotation.y);
    27.     }
    28. }
    Camera Script:
    Code (CSharp):
    1. public class CameraCollider : MonoBehaviour
    2. {
    3.     public Transform referenceTransform;
    4.     public float collisionOffset = 0.2f;
    5.  
    6.     Vector3 defaultPos;
    7.     Vector3 directionNormalized;
    8.     Transform parentTransform;
    9.     float defaultDistance;
    10.  
    11.     void Start()
    12.     {
    13.         defaultPos = transform.localPosition;
    14.         directionNormalized = defaultPos.normalized;
    15.         parentTransform = transform.parent;
    16.         defaultDistance = Vector3.Distance(defaultPos, Vector3.zero);
    17.     }
    18.  
    19.     void FixedUpdate()
    20.     {
    21.         Vector3 currentPos = defaultPos;
    22.         RaycastHit hit;
    23.         Vector3 dirTmp = parentTransform.TransformPoint(defaultPos) - referenceTransform.position;
    24.         if (Physics.SphereCast(referenceTransform.position, collisionOffset, dirTmp, out hit, defaultDistance))
    25.         {
    26.             currentPos = (directionNormalized * (hit.distance - collisionOffset));
    27.         }
    28.  
    29.         transform.localPosition = Vector3.Lerp(transform.localPosition, currentPos, Time.deltaTime * 15f);
    30.     }
    31. }
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    What actually doesn't work? What are your expected results, and what are you seeing instead? Why would you want collision on a camera?
     
    Yoreki likes this.
  3. Trekko

    Trekko

    Joined:
    Jan 24, 2018
    Posts:
    21

    (https://i.ibb.co/D5vxKfR/cameras.gif)
    TOP: smooth camera but camera colliding doesn't work
    BOTTOM: worse camera but with working collision
     
  4. MaximumTre

    MaximumTre

    Joined:
    Nov 28, 2015
    Posts:
    163
    Unless you just want to do it yourself from scratch, I suggest learning how to use Cinemachine. It's free, does some awesome stuff with cameras, and has a camera collider that works pretty well.