Search Unity

Tips on third person camera

Discussion in 'Scripting' started by Jonnis93, Feb 17, 2019.

  1. Jonnis93

    Jonnis93

    Joined:
    Feb 11, 2019
    Posts:
    6
    Hello! I need some tips/help with my camera script.
    I have set up a simple third person camera that is fixed to stay behind the character and turn with it. So far so good. The next step was to stop it from clipping trough walls etc. I added a raycast and got it to work. The only thing is that when the character walks away from the obstacle the camera starts to jump back and fourth. I guess it it because it is trying to go back to the original position. As I said I have a really simple script and I know that really good cameras are allot more complicated but I am just staring out so I want to keep it simple for now.
    Is there a way to add min/max distance to this so it does not have to go from one distance to the other? Just stay somewhere in between?
    Code (CSharp):
    1. public class PlayerCamera : MonoBehaviour
    2. {
    3.     private float distanceAway = 4f;
    4.     private float distanceUp = 2f;
    5.     private float smooth = 8f;
    6.     private Transform follow;
    7.     private Vector3 targetPosition;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         follow = GameObject.FindWithTag("Player").transform;
    13.     }
    14.  
    15.     void LateUpdate()
    16.     {
    17.         targetPosition = follow.position + Vector3.up * distanceUp - follow.forward * distanceAway;
    18.         transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smooth);
    19.         transform.LookAt(follow);
    20.  
    21.         RaycastHit hit;
    22.         Vector3 backward = transform.TransformDirection(Vector3.back);
    23.         Debug.DrawRay(transform.position, backward, Color.green);
    24.  
    25.         if (Physics.Raycast(transform.position,(backward), out hit, 4))
    26.         {
    27.             distanceAway = 1f;
    28.         }
    29.         else
    30.         {
    31.             distanceAway = 4f;
    32.         }
    33.  
    34.     }
    35. }
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Hi,

    Instead of this:
    Code (csharp):
    1. if (Physics.Raycast(transform.position,(backward), out hit, 4))
    2. {
    3.     distanceAway = 1f;
    4. }
    5. else
    6. {
    7.     distanceAway = 4f;
    8. }
    try this:
    Code (csharp):
    1. if (Physics.Raycast(transform.position,(backward), out hit, 4))
    2. {
    3.     distanceAway = hit.distance;
    4. }
    5. else
    6. {
    7.     distanceAway = 4f;
    8. }
    You could add other tweaks but I'm just trying to keep the code as close to yours as possible. Instead of jumping between 1 and 4 units away, it will smoothly adjust between them.
     
  3. Jonnis93

    Jonnis93

    Joined:
    Feb 11, 2019
    Posts:
    6
    That worked really well! Thank you so much! :D
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Happy to help!