# Tips on third person camera

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?

``````public class PlayerCamera : MonoBehaviour
{
private float distanceAway = 4f;
private float distanceUp = 2f;
private float smooth = 8f;
private Transform follow;
private Vector3 targetPosition;

// Start is called before the first frame update
void Start()
{
follow = GameObject.FindWithTag("Player").transform;
}

void LateUpdate()
{
targetPosition = follow.position + Vector3.up * distanceUp - follow.forward * distanceAway;
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smooth);
transform.LookAt(follow);

RaycastHit hit;
Vector3 backward = transform.TransformDirection(Vector3.back);
Debug.DrawRay(transform.position, backward, Color.green);

if (Physics.Raycast(transform.position,(backward), out hit, 4))
{
distanceAway = 1f;
}
else
{
distanceAway = 4f;
}

}
}
``````

Hi,

Instead of this:

``````if (Physics.Raycast(transform.position,(backward), out hit, 4))
{
distanceAway = 1f;
}
else
{
distanceAway = 4f;
}
``````

try this:

``````if (Physics.Raycast(transform.position,(backward), out hit, 4))
{
distanceAway = hit.distance;
}
else
{
distanceAway = 4f;
}
``````

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.

That worked really well! Thank you so much!

Happy to help!