Search Unity

Camera 2D Follow the player

Discussion in '2D' started by RoyalCoder, Sep 8, 2014.

  1. RoyalCoder

    RoyalCoder

    Joined:
    Oct 4, 2013
    Posts:
    301
    Hi friends, I'm trying to make my first 2D Running Game and I would like to know how to make a fixed Following Camera (EX: The Player position to be always on the bottom left) I made a graphic example ;)

    I have this script to fixing camera on player:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PlayerCam: MonoBehaviour {
    4.     public Transform player;
    5.     void Update ()
    6.     {
    7.         transform.position = new Vector3 (player.position.x + 7, 5, -20);
    8.     }
    9. }
    But I want the camera to follow the player on jumping moving on Y axises....any suggestion for doing this please ;)
     
  2. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    Look at the line of code in your Update method. It is positioning the camera horizontally in relation to the player's position. So... with that pattern in place and working... how do you think you could change the hard-coded value of 5 to achieve the tracking of the player's vertical position...
     
  3. RoyalCoder

    RoyalCoder

    Joined:
    Oct 4, 2013
    Posts:
    301
    Problem solved ....the finnal code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraJucator : MonoBehaviour {
    5.  
    6.     public int yOffset = 5;
    7.     private Transform player;
    8.  
    9.     void Awake()
    10.     {
    11.         player = GameObject.FindGameObjectWithTag("Player").transform;
    12.     }
    13.  
    14.     void Update ()
    15.     {
    16.         transform.position = new Vector3(player.position.x + 7, player.position.y + yOffset, -30);
    17.     }
    18. }
     
    Stony05 and GarBenjamin like this.
  4. Tutelage Systems

    Tutelage Systems

    Joined:
    Jun 7, 2013
    Posts:
    6
    @D0R1N that is pretty close to what I use.

    I would make the script a little more robust and allow the script to track other objects by passing in the reference to what you want the camera to track

    Code (CSharp):
    1. public GameObject trackableObject;
    and then maybe just using a vector 3 for all offsets

    Code (CSharp):
    1. public Vector3 offsetPositions;
    Finally in your update for the camera you transform would be something like.

    Code (CSharp):
    1. void Update()
    2. {
    3.   // Find our object's position
    4.   Vector3 trackablePosition = trackableObject.position;
    5.  
    6.   // add offsets
    7.   trackablePosition.x += offsetPositions.x;
    8.   trackablePosition.y += offsetPositions.y;
    9.   trackablePosition.z += offsetPositions.z;
    10.  
    11.   // Let our camera use it
    12.   transform.position = trackablePosition;
    13. }
    This way you can just continue to reuse the code over and over again.
     
    RoyalCoder likes this.
  5. videoanime

    videoanime

    Joined:
    Sep 28, 2014
    Posts:
    42
    Just to add something, if you want to add height or distance on the "X" axis, add values to the position.x or position.y with all this my camera works excellent :D
     
  6. RoyalCoder

    RoyalCoder

    Joined:
    Oct 4, 2013
    Posts:
    301
    Hi guys,
    I wonder how to make the camera not following the player on Y axis only if the player make the second jump or get's a specific altitude on Y, any ideas how to achieve this ?!