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

如何让GameObject继承父物体位置但保持世界空间旋转?

实现GameObject跟随父物体位置但保持自身世界旋转的方法

有两种常用方案可以实现这个需求:

方案一:通过脚本实时同步位置

在目标GameObject上挂载如下脚本,它会在每一帧LateUpdate阶段同步父物体的世界位置,同时保留自身的世界旋转角度:

using UnityEngine;

public class FollowParentPositionKeepRotation : MonoBehaviour
{
    private Transform parentTransform;
    private Quaternion targetWorldRotation;

    void Start()
    {
        parentTransform = transform.parent;
        // 记录初始的世界旋转
        targetWorldRotation = transform.rotation;
    }

    void LateUpdate()
    {
        if (parentTransform == null) return;
        
        // 同步父物体的世界位置
        transform.position = parentTransform.position;
        // 保持自身世界旋转不变
        transform.rotation = targetWorldRotation;
    }
}

如果需要允许后续手动修改旋转,可将targetWorldRotation的赋值逻辑调整到需要固定旋转的时机,而非仅在Start中赋值。

方案二:使用Unity内置约束组件

无需编写脚本,直接用Unity的Position Constraint组件实现:

  1. 选中目标GameObject,添加Component > Layout > Position Constraint组件
  2. 在组件的Sources列表中添加父物体
  3. 确保组件未勾选旋转相关约束选项(默认状态即可)
  4. 将Weight设为1,让位置完全跟随父物体

这样目标物体会自动跟随父物体的位置,同时自身世界旋转不受父物体影响。

输入图片描述
输入图片描述

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 14:10:49