- Home /
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);
Answer by ErikWelling · May 11, 2019 at 09:04 AM
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);
Answer by de_vas1995 · May 13, 2019 at 03:56 AM
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);
Your answer
![](https://koobas.hobune.stream/wayback/20220612201148im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to reference GameObjects to GUIButton??? 1 Answer
How Do I Make Camera Follow Sprite 2.5D 0 Answers
Keep camera at certain distance from character when rotating 3 Answers
How to get the forward vector normal to the camera's forward vector regardless of camera pitch 1 Answer
camera follow - no rotation 0 Answers