I want the camera to stop at the floor but it keeps going through it
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().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!!!
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().bounds.size.y/2) + cameraBounds.y;
Vector2 temp = player.transform.position;
if (player.transform.position.y < minYPos){
temp.y = minYPos;
}
this.transform.position = temp;
}
}
}
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ââ
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
