Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Camera Movement for my Platformer

Discussion in 'Scripting' started by Birkl, Dec 13, 2016.

  1. Birkl

    Birkl

    Joined:
    Dec 13, 2016
    Posts:
    4
    When working on one of my projects, a 2D platformer, I ran into a little coding problem I don't quite know how to solve.
    I know how to make the camera move along with the player all the time but I don't want that. I want the camera to move only in x and stay on the same y and only change that when the player get's to the top or bottom of the screen
    I tried transform.position.x but it didn't quite work as I wanted it to, meaning it produced a compiler error.
    So if any of you could help me that would be really great. Thanks.
     
  2. luke_2

    luke_2

    Joined:
    Nov 20, 2012
    Posts:
    29
    You can't amend a transform's positional x,y or z individually, you have to adjust the whole property. i.e. create/amend a Vector3 and assign that to the camera.

    Some example pseudocode:

    Vector3 cameraPos=camera.transform.position;
    cameraPos.x=player.transform.position.x;
    camera.transform.position=cameraPos;
     
    Birkl and Fabian-Haquin like this.
  3. Birkl

    Birkl

    Joined:
    Dec 13, 2016
    Posts:
    4
    Thank's.
    I can work with that.