You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity跨平台发布:9:18宽高比下小行星生成适配问题求助

Fixing Asteroid Clipping on 9:18 Aspect Ratio Devices in Unity

Hey there! Let's get that asteroid clipping issue sorted for those tall 9:18 screens (like the Samsung S8 or LG G6). The core problem here is your fixed X spawn range (-2.4f to 2.4f) — this works for standard aspect ratios, but when the screen is narrower (vertical 9:18), that range extends beyond the visible screen edges, causing asteroids to get cut off.

Instead of hardcoding values, we can calculate a dynamic spawn range based on the current screen's dimensions and your camera settings. Here are two reliable solutions depending on your camera type:

Solution 1: For Orthographic Cameras (Most Common for 2D Games)

If you're using an orthographic camera (standard for 2D asteroid games), we can use the camera's orthographicSize and aspect ratio to compute the exact visible width in world units:

// Get a reference to your main camera (cache this in Awake/Start for better performance)
Camera mainCamera = Camera.main;

// Calculate half the visible world height (from camera center to top edge)
float halfWorldHeight = mainCamera.orthographicSize;
// Calculate half the visible world width using the screen's aspect ratio (width/height)
float halfWorldWidth = halfWorldHeight * mainCamera.aspect;

// Generate a spawn X position within the visible screen, with a small margin to avoid edge clipping
float spawnX = Random.Range(-halfWorldWidth + 0.2f, halfWorldWidth - 0.2f);

// Spawn the asteroid with the dynamic X position
Instantiate(asteroid1prefab, new Vector3(spawnX, 6, 0), Quaternion.identity);

How This Works:

  • orthographicSize gives you the distance from the camera's center to the top of the visible screen in world units.
  • Multiplying by mainCamera.aspect (screen width / screen height) converts that height-based value to the corresponding width, ensuring we only spawn asteroids within the visible area regardless of the screen's aspect ratio.
  • The +0.2f and -0.2f add a small buffer so asteroids don't spawn directly on the screen edge (adjust this value to match your game's visual style).

Solution 2: Universal Approach (Works for Orthographic or Perspective Cameras)

If you're using a perspective camera, or want a solution that works for any camera type, use ScreenToWorldPoint to convert screen pixel edges to world coordinates:

Camera mainCamera = Camera.main;

// Calculate the distance between the camera and your asteroid's Z position
// (if your camera is at z=-10 and asteroids spawn at z=0, this would be 10)
float cameraToAsteroidDistance = Mathf.Abs(mainCamera.transform.position.z - 0);

// Get world coordinates for the left and right screen edges
float leftEdgeX = mainCamera.ScreenToWorldPoint(new Vector3(0, Screen.height / 2, cameraToAsteroidDistance)).x;
float rightEdgeX = mainCamera.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height / 2, cameraToAsteroidDistance)).x;

// Spawn with a small margin from edges
float spawnX = Random.Range(leftEdgeX + 0.2f, rightEdgeX - 0.2f);

Instantiate(asteroid1prefab, new Vector3(spawnX, 6, 0), Quaternion.identity);

Key Notes:

  • The cameraToAsteroidDistance ensures the ScreenToWorldPoint calculation uses the correct depth to get accurate world coordinates.
  • This method automatically adapts to any screen size or aspect ratio, including when the player rotates their device.

Bonus Tip: Handle Screen Rotations

If your game supports rotating between portrait and landscape, you'll want to update the spawn range whenever the screen size changes. Add this to your script to recalculate values on rotation:

private void OnRectTransformDimensionsChange()
{
    // Re-run the halfWorldWidth/edge calculations here, or call a dedicated update function
}

Or check for screen size changes in Update() (though OnRectTransformDimensionsChange is more efficient for UI-related size changes).


内容的提问来源于stack exchange,提问作者Marijan Klarić

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:34:06