Main Camera follows player?
I have a script attached to my main player, and I want to set the Main Camera to follow behind my player in the same script. Is there some way to do this by using the players position minus 1.5 for the cameras position?
I think I need to use a target variable correct? How would I do that?
$$anonymous$$aybe import the Character Controller and use that...
Answer by Aspirer · Nov 09, 2013 at 01:52 AM
All you need to do, is make the Main Camera a child of the Main Character object (drag and drop in the hierarchy menu.) Then, set the camera's transform to (0, 0, -1.5).
As a child, the camera will move when the player moves, and it's position will use the player's position, modified by the -1.5.
Meaning, if your player is on X: 0, Y: 0, Z: 0, the camera will be X:0, Y:0, Z: -1.5. If the player moves to 100, 100, 100, the camera will be 100, 100, 103.5
No need for any scripting to do this.
I know its been a long time but but i wanted to ask if you could tell me what im doing wrong. I set it under my played but i cant move it along z and now its floating over my players head
Answer by Huacanacha · Nov 09, 2013 at 02:01 AM
Take a look at the SmoothFollow in the Standard Assets scripts, which is included with the free version of Unity. That allows you to set a follow distance and height, and will smoothly follow your character around.
You apply this script to the camera, and use the inspector to set the follow distance, height, and target (the player game object in your case).
Note: if you don't install the standard assets with Unity you can download them from the Asset Store.
I have a small question, does it consider rotation? I mean, if a character changes the direction it's facing, the camara will do the same?
In order to rotate you will need to start scripting some.
I created a script a while ago that rotates the camera with the player it's pretty simple. I'll go look it up for you later today and try to explain it.
Yes, with SmoothFollow the camera will follow the rotation of the player. The difference between using this script and just parenting the camera on the target transform (i.e. making the camera a child of the player) is that the movement will be damped/smoothed which helps avoid sudden or jerky camera movement.
@Troas we are already talking about a script. Unity provides standard assets for basic camera scripting that you can just use and set the parameters on.
Answer by argertboja · Jun 21, 2017 at 03:35 PM
@aidenkael You don't have to do this by scripting because if you make your camera a child of the player it will automatically follow the player. However if you want to make it by scripting you can use the below C# code:
void Update () {
// Temporary vector
Vector3 temp = player.transform.position;
temp.x = temp.x - x;
temp.y = y;
temp.z = temp.z - z;
// Assign value to Camera position
transform.position = temp;
}
where x, y, z are the variables which define the difference of the camera position to that of the player.
Answer by StrawProductions · Feb 23, 2021 at 08:58 PM
hey im new to unity, exactly which XYZ things do i hav to change for the camera, i just see three options @Aspirer