Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
3
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 ?

11 Replies

· Add your reply
  • Sort: 
avatar image
16
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 6 · 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

Show more comments
avatar image
5

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 2 · 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 PythoJavaCoder · Apr 10 at 10:44 PM 0
Share

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.

avatar image
1

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

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
-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
  • 1
  • 2
  • 3
  • ›

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.

Follow this Question

Answers Answers and Comments

21 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 avatar image avatar image avatar image

Related Questions

Lock main camera on x-axis 2 Answers

How do i get a camera to follow a Bird/Plane smoothly? 2 Answers

How to do a plane follower? 3 Answers

Camera and Player position 0 Answers

Main Camera child under player, causes flip 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges