1
votes

I am trying to make a 2D game where a ball bounces off the edge of the camera in Unity. Now, if I understand it right, Unity is more, you make and environment, and then align the camera, not make the environment to fit the camera, right?

I could get the position of the ball and then see if its greater than a certain value and then apply a force, which is what I was doing, but with different devices, the screen ratio changes, so that doesn't work. Is there a way to detect if it just hits the edge of the camera/view or some percentage unit that I could use? With this game, the camera moves up and down with the ball so only the left and right sides would need to be "bouncy".

When I looked this up, some people suggested using an a barrier that was set the the edge the of the camera. With all the scaling and ratio aspect changes, I think it would be easier just to detect the edge, whether using some method to get the screen width in pixels, or some other way.

2
You can get the resolution using Screen.width and Screen.heightSavaria
@Savaria and this is in any script file (think the technical term is class), like you don't have to go under the camera, and its in pixels?Jack
You can use IsVisibleFrom to see if your object is in the camera frustum.Ali Kanat
@AliKanat, Interesting, do you know if it triggers when it touches the edge or goes completely out?Jack
The documentation is on the Unity Scripting API (link) Yes, it is in pixels and no it's about the game's window, not a specific cameraSavaria

2 Answers

0
votes
var dist = (this.transform.position 
- Camera.main.transform.position).z;

var leftBorder = 
Camera.main.ViewportToWorldPoint(new 
Vector3(0,0,dist)).x;
var rightBorder = 
Camera.main.ViewportToWorldPoint(new 
Vector3(1,0,dist)).x;
var topBorder = 
Camera.main.ViewportToWorldPoint(new 
Vector3(0,0,dist)).y;
var bottomBorder = 
Camera.main.ViewportToWorldPoint(new 
Vector3(0,1,dist)).y;

Vector3 playerSize = 


this.gameObject. 
renderer.bounds.size;

this.transform.position = new 
Vector3 (

Mathf.Clamp(this. 
transform.position.x, 
leftBorder + playerSize.x/2, 
rightBorder - playerSize.x/2),
Mathf.Clamp 
(this.transform.position.y, 
topBorder + playerSize.y/2, 
bottomBorder - playerSize.y/2),
this.transform.position.z

This is what i use. This was an answer at unity answer.

0
votes

One approach would be to transform the ball position into screen position, and then verify if it's going off screen (if the position has a x or y below 0 or a x greater than the screen width or a y greater than the screen height).

Vector2 screenPosition = Camera.main.WorldToScreenPoint(this.transform.position);
if((screenPosition.y > Screen.height) || (screenPosition.y < 0f) || (screenPosition.x > Screen.width) || (screenPosition.x <0f))
{   
    screenPosition.x = Mathf.Clamp(screenPosition.x, 0f, Screen.width);
    screenPosition.y = Mathf.Clamp(screenPosition.y, 0f, Screen.height);
    Vector3 newWorldPosition = Camera.main.ScreenToWorldPoint(screenPosition);
    this.transform.position = new Vector2(newWorldPosition.x, newWorldPosition.y);
    Move();
}
private void Move()
{
    velocity *= -1f;
    rigidBody.velocity = velocity;
}

In this example, the script will check if the ball goes of screen, and if it does, clamps the position between the screen edges and changes the velocity so the ball will bounce to the opposite direction. Note in the GIF below that if I change the aspect ratio, the ball still bounces when touching the edges.