Note in the GIF below that if I change the aspect ratio, the ball still bounces when touching the edges. 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. = new Vector2(newWorldPosition.x, newWorldPosition.y) Vector3 newWorldPosition = (screenPosition) A collider doesn’t need to be exactly the same shape as the object’s mesh - a rough approximation is often more efficient and indistinguishable in gameplay. ScreenPosition.y = Mathf.Clamp(screenPosition.y, 0f, Screen.height) The Area Effector 2D applies forces within an area defined by the attached Collider An invisible shape that is used to handle physical collisions for an object. ScreenPosition.x = Mathf.Clamp(screenPosition.x, 0f, Screen.width) ![]() ![]() If((screenPosition.y > Screen.height) || (screenPosition.y Screen.width) || (screenPosition.x <0f)) ![]() 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).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |