- Home /
Keep camera at certain distance from character when rotating
Hello. I'm trying to create a third person controller by myself for training purposes. Everything has gone well so far, but i'm stuck at making the camera follow the player at a certain distance (5 units), no matter it's orientation around the player. I know there are a couple of other threads, which I have read. But either they are not explanatory enough, or don't solve my problem.
I know I can get the distance from the camera to the character by subtracting the camera's position from the player's position and getting the magnitude from that Vector3. However, I do not know how to apply this distance to the camera's position, since I assume that you can't add a float to a Vector3.
A detailed explanation of how to solve this would be greatly appreciated.
Thanks Alex
I think you want to multiply ins$$anonymous$$d of add a float to a Vector3. scalar Vector3 is equivalent to new Vector(vector3.x scalar, vector3.y scalar, vector3.z scalar);
I'm sorry, I didn't understand that at all. What is a scalar?
It's just a magnitude or an integer. Vector3(0,0,10) = 10 * Vector(0,0,1)
As stated below, the camera's position and angle is being manipulated through transform.RotateAround(player). It is not supposed to be fixed to a point behind the player, but I still want it to keep a certain distance from the player when it moves.
Answer by LyanApps · Apr 02, 2013 at 07:38 PM
I would say try thinking your method in reverse. Instead of trying to find the distance from the camera to the player object and then applying that. You already know what distance you want, and I assume you know what position around the player you want your camera to be positioned at. So you should think about positioning the camera based on the player's location.
//Distance away from player you want to be at.
var distance = 10.0f;
//Direction from the player you want to be at.
//Use a unit vector which can be found from any Vector3 using .Normalize
//This vector is pointing behind and above the player at a 45deg angle
var direction = new Vector3(-10, 10, 0);
//distance * direction. normalized(); scales the unit vector to a constant size of distance behind the player.
camera.position = player.position + distance * direction. normalized();
Oh, I'm sorry. I forgot to mention that the camera is being rotated around the player using transform.RotateAround, when the right stick is pushed right, left, up or down. This means that the direction from the player is not always the same...
So can you have an empty game object that's position is equal to the player's position. Rotate that using your transform.RotateAround. Then in the given code change: var direction = go.forward;
If you think about it, what you're doing is putting your camera on a spherical plane around your player object. Using an empty GameObject would be equivalent of defining the center point of your sphere. The last line in the given code would essentially cast a ray out from the center of the sphere in direction and distance to map to a point where your camera should be positioned.
Answer by sparkzbarca · Apr 02, 2013 at 08:31 PM
get the slope of the line of the from the player to the camera normalize
multiply by 5 assign that to the camera
that'd be done as
camera.transform.position = (camera.transform.position - player.transform.position).normalized * 5;
or
get the slope of the line from player to player to the camera use that as a ray so you have origin and direction
get the point 5 units along the ray ray.point(5)
assign that to the camera
mark as answered please
Answer by mhtraylor · Apr 02, 2013 at 08:49 PM
You could use the very useful Mathf.Clamp
method on the camera's transform. For instance, when moving the camera you would "clamp" its position to always be at your specified distance (e.g. 5 units in your example). Mathf.Clamp in the Scripting Reference
Your answer
Follow this Question
Related Questions
How do i lock the position of the camera above the player relative to the origin point? 0 Answers
nullifying targetObject after a Smooth LookAt Transition 2 Answers
Player rotation = Camera Rotation 0 Answers
How can i make my crosshair drag behind the camera? 0 Answers
Locking world rotation of child 1 Answer