Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Checking camera collision

Discussion in 'Scripting' started by Sock-Eater, Sep 30, 2023.

  1. Sock-Eater

    Sock-Eater

    Joined:
    Aug 21, 2023
    Posts:
    2
    I have this script that rotates the camera around the player using transform.rotatearound and transform.lookat.
    Code (CSharp):
    1. playercamera.RotateAround(centerPoint.position, playercamera.up, turnerX);
    2. playercamera.RotateAround(centerPoint.position, playercamera.right, -turnerY);
    3. playercamera.LookAt(centerPoint);
    4. CheckCameraCollision(playercamera);
    I ran into the problem that the camera clips through walls so I added a function to deal with to try to avoid that
    Code (CSharp):
    1. Vector3 desiredPosition = cam.position;
    2.         RaycastHit hit;
    3.         if(Physics.Linecast(player.transform.position, desiredPosition, out hit, ~layertoignore))
    4.         {
    5.             //Camera distance minmax.x is lowest and minmax.y is max
    6.             cameraZoom = Mathf.Clamp(hit.distance, cameraDistanceMinMax.x, cameraDistanceMinMax.y);
    7.         }
    8.         else
    9.         {
    10.             cameraZoom = cameraDistanceMinMax.y;
    11.         }
    12.         Vector3 zoomVector = -playercamera.forward * zoom;
    13.         playercamera.position = centerPoint.position + zoomVector;
    In unity my player and camera are seperate objects. When I use this script sometimes it will do what its supposed to do and zoom in closer to the player but sometimes the camera doesn't zoom in more, is there a fix for this? Also I'm running the camera script in LateUpdate()

    Whats supposed to happen:

    What happens when it glitches:
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,410
    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.

    There's even a dedicated forum: https://forum.unity.com/forums/cinemachine.136/

    If you insist on making your own camera controller, the simplest way to do it is to think in terms of two Vector3 points in space: where the camera is LOCATED and where the camera is LOOKING.

    Code (csharp):
    1. private Vector3 WhereMyCameraIsLocated;
    2. private Vector3 WhatMyCameraIsLookingAt;
    3.  
    4. void LateUpdate()
    5. {
    6.   cam.transform.position = WhereMyCameraIsLocated;
    7.   cam.transform.LookAt( WhatMyCameraIsLookingAt);
    8. }
    Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations.
     
  3. Sock-Eater

    Sock-Eater

    Joined:
    Aug 21, 2023
    Posts:
    2
    Found that the problem was that it doesn't check for the collision if there is no mouse input, i just made it always check in case the player is moving but not the camera
     
    Kurt-Dekker likes this.
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,117
    You may not need to use LookAt when using transform.RotateAround as it sets both the position and the rotation of the transform.

    The script below assumes the camera is a child of the player object.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraOrbit : MonoBehaviour
    4. {
    5.     float idealDistance;
    6.    
    7.     void Start()
    8.     {
    9.         idealDistance=Vector3.Distance(transform.parent.position,transform.position);
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.         Vector3 pos=transform.parent.position+transform.parent.up;
    15.         transform.RotateAround(pos,Vector3.up,Input.GetAxis("Mouse X"));
    16.         transform.RotateAround(pos,transform.right,Input.GetAxis("Mouse Y"));
    17.         if (Physics.Raycast(pos,transform.position-pos,out RaycastHit hit,idealDistance))
    18.             transform.position=hit.point;
    19.         else
    20.             transform.position=Vector3.MoveTowards(transform.position,pos+(transform.position-pos).normalized*idealDistance,20*Time.deltaTime);
    21.     }
    22. }
     
    Last edited: Oct 3, 2023