Опыт первого лица в RealityKit - PullRequest
0 голосов
/ 29 марта 2020

Я довольно новичок в Swift и только начал играть с RealityKit и ARKit. Я работаю над личным проектом, где я хотел бы, чтобы 3D-объект прикреплял к камере от первого лица. Похоже на AR Angry birds или любую игру FPS. Я видел несколько примеров в SceneKit или SpriteKit, я уверен, что это просто неправильное понимание того, как работает привязка сущностей.

Мой главный вопрос:

  • Как бы я go о прилипании объекта реальности, который я создал в реальности Composer к камере от первого лица? Я хочу создать Сцена Реальности, в данном случае вооруженную пушку и при нажатии на нее выстрелы.

Ниже приведен код для моего ViewController

extension ViewController: ARSessionDelegate
{
    func session(_ session: ARSession, didUpdate frame: ARFrame)
    {
          guard let arCamera = session.currentFrame?.camera else { return }
          // Probably where I update the location of my reality experience
    }
}

class ViewController: UIViewController
{
    @IBOutlet var arView: ARView!

    override func viewDidLoad()
    {
        super.viewDidLoad()
        arView.session.delegate = self

        // Load the "ArmCannon" scene from the "Experience" Reality File
        let armCannonAnim = try! Experience.loadArmcannon()

        // Create Anchor to anchor arm cannon to
        let anchor = AnchorEntity(.camera)
        anchor.transform = arView.cameraTransform

        // Add the anchor to the scene
        arView.scene.addAnchor(anchor)

        // Setup tap gesture on arm cannon
        let tapGesture = UITapGestureRecognizer(target: self, action:#selector(onTap))
        arView.addGestureRecognizer(tapGesture)

        // Add the the cannon animation to arView
        arView.scene.anchors.append(armCannonAnim)

    }

    @IBAction func onTap(_ sender: UITapGestureRecognizer)
    {

        let tapLocation = sender.location(in: arView)

        // Get the entity at the location we've tapped, if one exists
        if let cannonFiring = arView.entity(at: tapLocation)
        {
            print(cannonFiring.name)
            print("firing Cannon")
        }
    }
}

, на который я смотрел и читать Отслеживать положение камеры с помощью RealityKit и Где находится .camera AnchorEntity?

1 Ответ

0 голосов
/ 30 марта 2020

Вместо:

arView.scene.anchors.append(armCannonAnim)

положить:

anchor.addChild(armCannonAnim)

Вам нужен этот armCannonAnim, чтобы быть дочерним элементом камеры, а объект anchor является якорем на камере преобразования. Это эквивалентно добавлению дочернего элемента в SceneKit к CameraNode.

...