2D Player bounds problem based on moving camera
On my 2D project, I have moving camera to right and has boundaries;
void LateUpdate()
{
Vector3 temp = transform.position;
//temp.x = playerTransform.position.x;
temp.y = playerTransform.position.y;
transform.position = temp;
transform.position += Vector3.right * Time.deltaTime * 2;
//Camera Boundries
transform.position = new Vector3(transform.position.x,
Mathf.Clamp(playerTransform.position.y, -9f, 6f), transform.position.z
);
}
And I set the camera boundaries for my player in my player controller script;
Vector2 minScreenBounds = Camera.main.ScreenToWorldPoint(new Vector2(0, 0));
Vector2 maxScreenBounds = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
transform.position = new Vector3(Mathf.Clamp(transform.position.x, minScreenBounds.x + 0.5f, maxScreenBounds.x - 0.6f), Mathf.Clamp(transform.position.y, minScreenBounds.y + 1f, maxScreenBounds.y - 0.3f));
Player boundaries code is in Update, it works for right,top and bottom but when Player goes to all the left it doesn't move, sometimes it moves and when player at all the way left player barely moves up and down, could you help me to fix what is wrong?
If I remove camera moving to the right code then I don't have this problem.
Thank you.
Your answer
Follow this Question
Related Questions
Regarding transform.position in the roll a ball tutorial 0 Answers
Using Camera.main.ViewportToWorldPoint to limit player movement in the Y direction 0 Answers
How to make an object go the direction it is facing? 0 Answers
Make object move back and forth 2 Answers
Moving GameObject a specific distance in the Z direction and back again - regardless of rotation 1 Answer