• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jack-Howard · Jan 18, 2015 at 12:30 AM · cameraplayercamera-movementplatformerfollow

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Jan 18, 2015 at 12:30 AM 0
Share

player.position.y ?

10 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

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
  }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wefjyl · Jul 07, 2016 at 12:02 PM 0
Share

i've been stuck on camera movement scripting for days, thank you so much for this, solved my problems in an instant!

avatar image Andi96 · Mar 03, 2017 at 06:20 AM 0
Share

Dude, you can´t imagine how much you´ve helped me Thanks a lot man!

avatar image Emberheart-Games · Feb 10, 2020 at 05:02 PM 0
Share

I would update camera position in LateUpdate() or even FixedUpdate() to make it even smoother.

avatar image Ognjen_2008 · May 13, 2020 at 11:21 PM 0
Share

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!

avatar image GGTotter · Aug 31, 2020 at 10:32 PM 0
Share

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

avatar image
3

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 alt text

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Luan240 · Aug 07, 2020 at 03:48 AM 0
Share

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

avatar image
-2

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by mnmwert · Jul 12, 2016 at 07:57 PM

Try this simple code?

Public Transform Player; private Vector3 offset;

void Update () { transform.position.x = Player.transform.position.x; transform.position.y = Player.transform.position.y; transform.position.z = offset.z; }

this should work.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Closed] SmoothFollow trouble 0 Answers

Multiple Targets Camera 1 Answer

Getting Camera to follow falling Dominoes 1 Answer

Camera follow player with code 3 Answers

Moving 3rd person player relative to orbiting camera 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges