Search Unity

How to make the camera stop at the floor

Discussion in '2D' started by DrunkenMonkeyProductions, Apr 20, 2017.

  1. DrunkenMonkeyProductions

    DrunkenMonkeyProductions

    Joined:
    Mar 29, 2016
    Posts:
    6
    I want the camera to stop at the floor but it keeps going through it Untitled 5.jpg
     
  2. clorxwetwipe

    clorxwetwipe

    Joined:
    Sep 28, 2015
    Posts:
    3
    Here is a solution :

    - Make a new script on your camera.
    - Initialize these three variables. We'll need them for future calculations.

    Vector2 cameraBounds; // Vector2 that holds half the width and half the height of the camera in World space
    GameObject floor; // bottom platform
    GameObject player; // player
    float minYPos; // The minimum position for the camera to be

    Create a new Vector2 variable named 'cameraBounds'. This vector2 determines the half the width and half the height of the camera in World space. You must also have the player and the floor held in a variable. For example, you can do this by applying tags to both objects, and fetching them in the camera script. Put these into the Start() of your script.:

    player = GameObject.FindGameObjectWithTag("player");
    floor = GameObject.FindGameObjectWithTag("floor");
    cameraBounds = Camera.main.ScreenToWorldPoint (new Vector2(Camera.main.pixelWidth, Camera.main.pixelHeight));

    Now that we have this information, we should run a simple check to see if the player is 'too close to the ground' for the camera to move, in a sense. This is also in your start function.

    minYPos = (floor.transform.position.y - floor.GetComponent<SpriteRenderer>().bounds.size.y/2) + cameraBounds.y;

    Now, we're going to see if the player's y position is less than our minimum y value. If it is, we'll update a temporary variable that holds the future position of the camera. This position is usually set to the player's position, but we don't want that to be the case all the time.

    // Ensures camera does not go below the player
    // Lets start by making a temporary Vector2, which is equal to the players position. Then we can modify it if the player is too low.
    Vector2 temp = player.transform.position;

    if (player.transform.position.y < minYPos) {
    temp.y = minYPos;
    }

    this.transform.position = temp;

    Please reply if you have questions!!!
     
  3. DrunkenMonkeyProductions

    DrunkenMonkeyProductions

    Joined:
    Mar 29, 2016
    Posts:
    6
    ok so if I did this correctly what do I do next

    using System;
    namespace Application
    {
    public class EmptyClass
    {
    var Vector2 cameraBounds; //Vector2 that holds half the width and half the height of the camera in World space
    var GameObject floor; //Bottem platform
    var GameObject player; //player
    float minYPos; //the minimum position for the camera to be
    void Start()
    {
    player = GameObject.FindGameObjectWithTag("CharacterRobotBoy");
    floor = GameObject.FindGameObjectWithTag("floor");
    cameraBounds = Camera.main.ScreenToWorldPoint (new Vector2(Camera.main.pixelWidth, Camera.main.pixelHight));
    minYPos = (floor.transform.position.y - floor.GetComponent<SpriteRenderer>().bounds.size.y/2) + cameraBounds.y;
    Vector2 temp = player.transform.position;

    if (player.transform.position.y < minYPos){
    temp.y = minYPos;
    }

    this.transform.position = temp;
    }
    }
    }
     
  4. DrunkenMonkeyProductions

    DrunkenMonkeyProductions

    Joined:
    Mar 29, 2016
    Posts:
    6
    when I try to put it in uinty I get this error "Assets/Standard Assets/2D/Scripts/CameraLock.cs(1,0): error CS1525: Unexpected symbol `u'"
     
  5. ThePanJake

    ThePanJake

    Joined:
    Dec 20, 2017
    Posts:
    1
    Do you really don't know how to solve this problem? I know i'm a little bit late but hey, just read the error message