- Home /
How to get camera to follow player 2d
Hi I just made a 2d endless runner and this is the code i used to get the camera to follow the player (its in C#)
using UnityEngine; using System.Collections;
public class CameraRunnerScript : MonoBehaviour {
public Transform player;
void Update ()
{
transform.position = new Vector3 (player.position.x + 6, 0, -10); // Camera follows the player but 6 to the right
}
}
it works fine following the player on the x axis but i was hoping someone could help me edit it so it follows the player on the y axis as well. thanks
Answer by Shinyclef · Jan 18, 2015 at 12:37 AM
Pretty easily achieved. Your code already demonstrates how to do it. Here's an updated version to make it a little easier for you. Just change the 'offset' values in the inspector to whatever you want. Something like (6, 6, -10) perhaps.
public Transform player;
public Vector3 offset;
void Update ()
{
transform.position = new Vector3 (player.position.x + offset.x, player.position.y + offset.y, offset.z); // Camera follows the player with specified offset position
}
i've been stuck on camera movement scripting for days, thank you so much for this, solved my problems in an instant!
Dude, you can´t imagine how much you´ve helped me Thanks a lot man!
I would update camera position in LateUpdate() or even FixedUpdate() to make it even smoother.
Hey!
I know its been like what - 5 years since you`ve posted this code?
and it still works as of 14/05/2020 in Unity 5.6.7f1. Thank you! I really struggled for the camera follow to work, but, with your code it works!
Again, thank you, cheers!
I have seen 4 different tutorials on camerafollow and in all of them you have to write like 10 lines of code plus drop the player on to the camera script. You are a certified good guy my dude
Answer by Parsnap1 · Jun 12, 2019 at 06:42 PM
Unity 2D doesn't have the Cinemachine tool like Unity 3D does so your first thought might go to code, but that's unnecessary. All you need to do to get the camera to track your player character is to make the camera object a child of the player character object. Any offset you want can easily be inputted into the transform component of your camera object after you make it a child
It works, but when i rotate my character, my camera rotates too. So when i go to the left it seems like im going to the right, and it always look like that! Thx for the help anyway
I tried it on my player, but whenever the player is rotated to the side, the controls would look weird. And the environment would look strange. Are there any other methods, or is that the only one? I'd like to know.
Answer by gpaulguilfoyle · Feb 04, 2020 at 10:34 PM
Check this out, it discusses camera smoothing, camera bounds and camera follow: unity camera follow player tutorial
Answer by omaromarsol · Oct 11, 2016 at 08:34 PM
Hi I'm trying to make a game of a sphere moving on 2D ground in the z-axis direction and when I try to follow the sphere with the camera it stops the movement of the ball to the right and left Here is my code for the movement: public Rigidbody rb; // Use this for initialization void Start() { rb = GetComponent(); rb.AddForce(Vector3.forward * 1, ForceMode.Impulse); }
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
rb.AddForce(Vector3.left * 1, ForceMode.Impulse);
rb.AddForce(Vector3.forward * 1, ForceMode.Impulse);
}
if (Input.GetKeyDown(KeyCode.D))
{
rb.AddForce(Vector3.right * 1, ForceMode.Impulse);
rb.AddForce(Vector3.forward * 1, ForceMode.Impulse);
}
if (Input.GetKey(KeyCode.Space))
{
Vector3 dir = new Vector3(-10f, 15f, 0f);
dir.Normalize();
this.gameObject.GetComponent<Rigidbody>().AddForce(dir * 50);
rb.AddForce(Vector3.forward * 1, ForceMode.Impulse);
}
Any help how can I can solve my problem in the z-axis direction please
Answer by juliancruz · Jul 11, 2016 at 03:54 PM
Please becareful using transform Directly on update . Instead use a reference to Prevent performance issues.