Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

2D Camera (Follow player vertically) Help Needed

Discussion in 'Scripting' started by SoftReckonStudios, Feb 19, 2021.

  1. SoftReckonStudios

    SoftReckonStudios

    Joined:
    Feb 19, 2021
    Posts:
    4
    Hello there. I humbly need help on my script. I have this script for making the 2D camera follow the player.
    It works perfectly on the horizontal (the x-axis) but not on vertical, such that when the player moves down below the camera in the level he goes completely off.
    I will be very much grateful if I can be assisted as I am very new to Unity. Thanks in advance.

    Heres my code:


    public class Camera_Controller : MonoBehaviour
    {
    public GameObject player;
    public float offset;
    private Vector3 playerPosition;
    public float offsetSmoothing;
    // Start is called before the first frame update
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
    playerPosition = new Vector3(player.transform.position.x, transform.position.y, transform.position.z);
    if (player.transform.localScale.x > 0f)
    {
    playerPosition = new Vector3(playerPosition.x + offset, playerPosition.y, playerPosition.z);
    }
    else
    {
    playerPosition = new Vector3(playerPosition.x - offset, playerPosition.y, playerPosition.z);
    }
    if ( player.transform.localScale.y < 0f)
    {
    playerPosition = new Vector3(playerPosition.x , playerPosition.y - offset, playerPosition.z);
    }
    transform.position = Vector3.Lerp(transform.position, playerPosition, offsetSmoothing * Time.deltaTime);
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    13,714
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    What is your thinking with checking that localScale? Is that just for deciding if I am facing left / right and adding the
    offset
    to move the camera ahead?

    If so, I don't think it is set up properly for up/down. Or possibly the offset should be two separate offsets, one for vertical, one for horizontal. Right now they are the same, and if you were in landscape, with a large lateral offset, the vertical might be so much it pushes the player off.

    Try setting the
    offset
    to zero and see if it follows properly, then try splitting it into two float offsets, one for horizontal, one for vertical. I would imagine the vertical one would be smaller than the lateral one, at least in landscape aspect.
     
  3. SoftReckonStudios

    SoftReckonStudios

    Joined:
    Feb 19, 2021
    Posts:
    4

    Thanks bro, and sorry for my late reply. I hadnt gotten back to this thread because I used Cinemachine and that solved my issue with cameras in my game. All the same thanks for the support. Cheers
     
    Kurt-Dekker likes this.
unityunity