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

RealityKit中如何沿Z轴移动模型?修复move方法调用错误

修复RealityKit中move方法调用错误

错误原因

你调用move方法时参数顺序不符合RealityKit的方法签名。RealityKit中Entity的move(by:relativeTo:duration:)方法参数顺序为:位移向量、相对参考实体、动画时长,而你把动画时长和相对实体的位置写反了,导致编译器找不到匹配的方法。

修复方案

方案1:调整参数顺序

把duration参数移到最后,修正后的调用代码:

model.move(by: SIMD3<Float>(x: 0, y: 0, z: -0.5), 
           relativeTo: nil, 
           duration: 0)

方案2:直接修改position(更高效)

如果不需要动画效果(duration:0),直接修改实体的position属性是更直接高效的方式,无需调用动画方法:

model.position += SIMD3<Float>(x: 0, y: 0, z: -0.5)

完整修正后的代码

import SwiftUI
import RealityKit

struct ARContainer: UIViewRepresentable {
    let anchor = AnchorEntity(world: [0, 0, -1])
    
    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero)
        arView.cameraMode = .ar                  
        
        let model = try! Entity.loadModel(named: "box.usdz")
        
        anchor.addChild(model)
        arView.scene.anchors.append(anchor)
        
        // 方案1:调整参数顺序的move调用
        model.move(by: SIMD3<Float>(x: 0, y: 0, z: -0.5), 
                   relativeTo: nil, 
                   duration: 0)
        
        // 或者用方案2:直接修改position
        // model.position += SIMD3<Float>(x: 0, y: 0, z: -0.5)
        
        return arView
    }
    
    func updateUIView(_ uiView: ARView, context: Context) { }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 06:34:58