# How do I make the camera follow a vector3? 3D

my player makes a raycast from the camera to the position of the mouse (the player looks at that hit) my intention is to calculate the midpoint between that hit and the player, and that the camera looks at that vector with a clamp so that it does not go to the infinite, the problem that is not happening locally to the player but the world, so if I move away from the center the camera stays behind and does not follow the player. it only works “well” when the player is in the center of the map

``````    Vector3 distance = _player.transform.position - _player.GetComponent<Player>().hitSave;
Vector3 midpoint = new Vector3(-distance.x / 2, transform.position.y, -distance.z / 2);

xLimit = Mathf.Clamp(midpoint.x, -4, 4);
zLimit = Mathf.Clamp(midpoint.z, -4, 4);

Vector3 follow = new Vector3(xLimit, transform.position.y, zLimit);

//Vector3 followTwo = new Vector3(midpoint.x, transform.position.y, midpoint.z);

transform.position = Vector3.Slerp(new Vector3(follow.x, transform.position.y, follow.z), follow,
smooth);
transform.eulerAngles = new Vector3(60, -90, 0);
``````

This is because ‘distance’ is a vector that shows a direction. For example:
_player.transform.position = (4,0,3) and
_player.GetComponent().hitSave = (2,0,3),
then distance = (2,0,0), which means that to go from player position to hit position you need to ADD 2 to the X value of player position, NOT make the X value of the player position EQUAL to 2.

so transform.position should be:

``````transform.position = Vector3.Slerp(new Vector3(transform.position.x + follow.x, transform.position.y, transform.position.z + follow.z), follow, smooth);
``````

I could solve it by changing the vectors 3 for a fictitious transform

Transform fictional;

fictional.position += GetComponentInParent().hitSave;

Vector3 test = fictional.InverseTransformPoint(myPlayer.transform.position);