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

自定义位置与旋转实例化预制体:对象位置倾斜问题求助

Fixing Tilted Instantiated Objects in Unity

Let's break down why your knights are tilting and how to get that neat, aligned arrangement you want:

The Root Causes

Looking at your code, two key issues are causing the tilted/offset placement:

  • You're explicitly setting a fixed Y-axis rotation of 42 degrees with Quaternion.Euler(new Vector3(0,42,0)) — this forces every instantiated knight to be rotated, which is exactly why they're tilted.
  • Your position calculation adds directly to the world X-axis, but if the parent object (the one this script is attached to) has any rotation, this will make the knights offset along a tilted axis instead of a straight line.

The Fix

Here's the adjusted code with clear explanations:

public Transform Knights;
void Start(){
    // Instantiate 6 copies of knights, 2 units apart (matching your original intent)
    float spacing = 2.0f; 
    for(int i = 0; i < 6; i++){
        // Calculate position in local space first, then convert to world space to account for parent rotation
        Vector3 localOffset = new Vector3(i * spacing, 0, 0);
        Vector3 worldPosition = transform.TransformPoint(localOffset);
        
        // Use the parent's rotation (or Quaternion.identity for no rotation) instead of fixed 42 degrees
        Instantiate(Knights, worldPosition, transform.rotation);
    }
}

Key Changes:

  1. Removed Forced Rotation: Instead of locking in (0,42,0), we use transform.rotation to match the parent object's orientation. If you want knights to face world forward regardless of the parent, swap this with Quaternion.identity.
  2. Proper Position Alignment:
    • We set spacing to 2.0f to match your comment about placing objects 2 units apart.
    • transform.TransformPoint() converts the local X-axis offset into world coordinates. This ensures knights line up straight even if the parent object is rotated.
  3. Simplified Position Logic: No need to manually add transform.position.x — TransformPoint handles combining the parent's position and local offset automatically.

Alternative: World-Space Straight Line

If you want knights to line up along the world X-axis no matter how the parent is rotated, use this simplified position calculation:

Vector3 worldPosition = new Vector3(transform.position.x + (i * spacing), transform.position.y, transform.position.z);

Keep the rotation set to either transform.rotation or Quaternion.identity based on your desired knight orientation.


内容的提问来源于stack exchange,提问作者NewProg

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:50:44