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

3rd Person Camera - Going from outdoors to indoors?

Discussion in 'Editor & General Support' started by Zexanima, Sep 11, 2014.

  1. Zexanima

    Zexanima

    Joined:
    Jan 2, 2014
    Posts:
    33
    I setup a 3rd person style camera on my player. However, being the dunce that I am, forgot about going from outdoors to indoors. My character walks inside, but his camera stays the same distance away, outside the building! Could anyone give me a hit as to how I could make the camera intelligently move closer to the character when they move indoors to prevent it from being outside the building?
     
  2. Chambers

    Chambers

    Joined:
    Apr 4, 2011
    Posts:
    70
    You want to look into Raycasts from the camera to check if it is colliding with any objects then move the camera to a position where it will be unobstructed. This is a relatively complicated problem but it has been tackled by several tutorials the one I would recommend is the 3DBuzz 3rd Person Character Controller series of videos; http://www.3dbuzz.com/training/view/3rd-person-character-system

    This video series helped me understand a lot of the underlying principles behind camera controllers and tackles the issue you are experiencing in a really neat way.
     
  3. Zexanima

    Zexanima

    Joined:
    Jan 2, 2014
    Posts:
    33
    Alright, thanks! I'll check it out. I've started to develop a solution on my own, using Raycast, but it's not working out so well haha.
     
    Chambers likes this.
  4. Zexanima

    Zexanima

    Joined:
    Jan 2, 2014
    Posts:
    33
    Before I watched those videos I went ahead and figured out my own solution (it might be the same as the video's, not sure). Its probably not he best but here it is for anyone who might happen across this thread in the future.
    Code (CSharp):
    1. public Camera mainCam;
    2.     public float hitBuffer = 0.5f;
    3.     float originalCamDis;
    4.     Vector3 originalCamPos;
    5.     Vector3 p1;
    6.     Vector3 p2;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         mainCam = Camera.main;
    11.         originalCamDis = Vector3.Distance (transform.position, mainCam.transform.position);
    12.         originalCamPos = mainCam.transform.localPosition;
    13.         p1 = transform.position;
    14.         p2 = originalCamPos;
    15.     }
    16.  
    17.     void CheckForCollision(){
    18.         p1 = transform.position;
    19.         p2 = transform.TransformPoint (originalCamPos);
    20.         RaycastHit hit = new RaycastHit();
    21.         if(Physics.Linecast(p1, p2, out hit)){
    22.             Vector3 newCamPos = transform.InverseTransformPoint(hit.point);
    23.             newCamPos = new Vector3(newCamPos.x, newCamPos.y - hitBuffer, newCamPos.z + hitBuffer);
    24.             mainCam.transform.localPosition = newCamPos;
    25.         } else {
    26.             mainCam.transform.localPosition = originalCamPos;
    27.         }
    28.     }
    29.    
    30.     // Update is called once per frame
    31.     void Update () {
    32.         Debug.DrawLine (p1, p2);
    33.         CheckForCollision ();
    34.     }
     
    Chambers likes this.