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

[Help] Camera clipping through walls (camera collide)

Discussion in 'Scripting' started by Felix-vg, Feb 1, 2019.

  1. Felix-vg

    Felix-vg

    Joined:
    Feb 1, 2019
    Posts:
    3
    Hi everyone! I'm developing my first game I'm having some troubles with my camera. It works nice, it rotates and move its position perfectly, but it keep going through walls and I'm not sure what I should do. It will be great if someone could shed some light on this matter, since I've been several days trying to code this. I'll leave here the code until now (unity 2018.3.2.f1, btw).

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraController : MonoBehaviour {
    6.  
    7.  
    8.     public Transform target;
    9.  
    10.     public Vector3 offset;
    11.  
    12.     public bool useOffsetValues;
    13.  
    14.     public float rotateSpeed;
    15.  
    16.     public Transform pivot;
    17.  
    18.     public float maxViewAngle;
    19.     public float minViewAngle;
    20.  
    21.     public bool invertY;
    22.    
    23.    
    24.     void Start () {
    25.        
    26.         if (!useOffsetValues)
    27.         {
    28.             offset = target.position - transform.position;
    29.         }
    30.        
    31.         pivot.transform.position = target.transform.position;
    32.        
    33.         pivot.transform.parent = null;
    34.        
    35.         Cursor.lockState = CursorLockMode.Locked;            
    36.     }
    37.  
    38.  
    39.    
    40.     void LateUpdate () {
    41.        
    42.         pivot.transform.position = target.transform.position;
    43.        
    44.         float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
    45.         pivot.Rotate(0, horizontal, 0);
    46.        
    47.         float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
    48.  
    49.         if (invertY)
    50.         {
    51.             pivot.Rotate(vertical, 0, 0);
    52.         }
    53.         else
    54.         {
    55.             pivot.Rotate(-vertical, 0, 0);
    56.         }
    57.  
    58.  
    59.         float desiredYAngle = pivot.eulerAngles.y;
    60.  
    61.         float desiredXAngle = pivot.eulerAngles.x;
    62.        
    63.         if (pivot.rotation.eulerAngles.x > maxViewAngle && pivot.rotation.eulerAngles.x < 180f)
    64.         {
    65.             pivot.rotation = Quaternion.Euler(maxViewAngle, desiredYAngle, 0);
    66.         }
    67.  
    68.         if (pivot.rotation.eulerAngles.x > 180f && pivot.rotation.eulerAngles.x < 360f + minViewAngle)
    69.         {
    70.             pivot.rotation = Quaternion.Euler(360f + minViewAngle, desiredYAngle, 0);
    71.         }
    72.        
    73.         Quaternion rotation = Quaternion.Euler(desiredXAngle, desiredYAngle, 0);
    74.         transform.position = target.position - (rotation * offset);
    75.        
    76.         if(transform.position.y < target.position.y)
    77.         {
    78.             transform.position = new Vector3(transform.position.x, target.position.y -.5f, transform.position.z);
    79.         }
    80.         transform.LookAt(target);
    81.     }
    82. }
    83.  

    Thanks!
     
  2. Felix-vg

    Felix-vg

    Joined:
    Feb 1, 2019
    Posts:
    3
    Bump! Still having this issue. Would be great if someone could help. Thanks!
     
  3. bentontr

    bentontr

    Joined:
    Jun 9, 2017
    Posts:
    39
    Basically you need to do a raycast (or spherecast, depending on the effect you want) between your player's position and the camera. If it hits something you need to adjust the camera to that point to avoid the obstacle. If it doesn't hit anything, no adjustment is required.
     
    Felix-vg and eses like this.
  4. Felix-vg

    Felix-vg

    Joined:
    Feb 1, 2019
    Posts:
    3
    Thank you! Since I am starting with coding in C# this sounds a bit difficult, but better than nothing for sure. Thanks for the advice, I'll see what I can do.