Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Camera collision to wall help

Discussion in 'Scripting' started by yesyesok, Jul 23, 2017.

  1. yesyesok

    yesyesok

    Joined:
    Apr 17, 2016
    Posts:
    13
    hey guys i'm trying to avoid camera collision to walls and always want my camera to look at my player

    so what i did is i made my character direction change based on mouse movement and made my camera change it's position based on players facing direction here is my camera rotation script using UnityEngine; using System.Collections;

    1. public class camScript : MonoBehaviour
    2. {
    3. public bool LockCursor;
    4. public GameObject myPlayerGameObject;
    5. public float DistanceFromPlayer;
    6. void Start ()
    7. {
    8. if (LockCursor)
    9. {
    10. Cursor.lockState = CursorLockMode.Locked;
    11. Cursor.visible = false;
    12. }
    13. }
    14. void Update ()
    15. {
    16. transform.position =myPlayerGameObject.transform.position - player.GetComponentInChildren<PlayerController>().GetPlayerFacing() * Distance;
    17. transform.LookAt(myPlayerGameObject.transform);
    18. }
    19. }
    it works perfectly fine expect when my player near walls and there is a wall in between my player and camera. I tried using ray but i was not getting a desired result. Can anyone sugget anything?

    sorry for my Bad English