# STUDIO: LookAt Rotation from a child object

**URL:** <https://forum.8thwall.com/t/studio-lookat-rotation-from-a-child-object/3427>\
**Category:** Technical Support\
**Tags:** studio\
**Created:** [November 3, 2024, 10:34pm UTC](https://forum.8thwall.com/t/studio-lookat-rotation-from-a-child-object/3427 "2024-11-03T22:34:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sabrina\_Cooke](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@Sabrina\_Cooke](https://forum.8thwall.com/u/Sabrina_Cooke)\
**Post date:** [November 3, 2024, 10:34pm UTC](https://forum.8thwall.com/t/studio-lookat-rotation-from-a-child-object/3427/1 "2024-11-03T22:34:43Z")

</div>

I have a child object, which I would like to `LookAt()` at the camera object which does not have a parent. The two objects are not related in the hierarchy.

e.g.

```auto
Parent
- Child1
 - Child2 (eye)
Camera (target)

```

The problem I am running into is if any parents have any position or rotation values, then everything is thrown off.

I’ve noticed that `ecs.Position.get()` returns the local position, and not the world position - I can’t seem to find a `WorldPosition` function. If one exists please send it my way 🙏 .

In the meantime I’ve attempted to make a quick and dirty one myself, but there is still some issues that maybe someone else can spot and remedy.

Functions:

```auto
function GetWorldPosition(world, eid) {
  if (eid) {
    const calculatedPosition = ecs.math.vec3.from(ecs.Position.get(world, eid))
    let objectParent = world.getParent(eid)

    while (objectParent) {
      const parentPosition = ecs.math.vec3.from(ecs.Position.get(world, objectParent))
      calculatedPosition.setPlus(parentPosition)
      objectParent = world.getParent(objectParent)
    }

    return calculatedPosition
  }
}

function GetWorldRotation(world, eid) {
  if (eid) {
    const calculatedRotation = ecs.math.quat.zero()
    let objectParent = world.getParent(eid)

    while (objectParent) {
      const parentRotation = ecs.math.quat.from(ecs.Quaternion.get(world, objectParent))
      calculatedRotation.setPlus(parentRotation)
      objectParent = world.getParent(objectParent)
    }

    return calculatedRotation
  }
}

```

Tick:

```auto
tick: (world, component) => {
    // Runs every frame.
    const {eid} = component
    const {rotateYOnly} = component.schema

    // Look at the camera.

    const cameraPosition = vec3.from(ecs.Position.get(world, world.camera.getActiveEid()))
    const objectWorldPosition = GetWorldPosition(world, eid)
   
    const lookAtQuaternion = quat.lookAt(objectWorldPosition, cameraPosition, vec3.up())
    
    // Undo the parents rotation
    lookAtQuaternion.setPlus(GetWorldRotation(world, eid))

    ecs.Quaternion.set(world, eid, lookAtQuaternion)
  }

```

---

<div class="post-metadata">

**Author:** ![Sabrina\_Cooke](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@Sabrina\_Cooke](https://forum.8thwall.com/u/Sabrina_Cooke)\
**Post date:** [November 3, 2024, 11:04pm UTC](https://forum.8thwall.com/t/studio-lookat-rotation-from-a-child-object/3427/2 "2024-11-03T23:04:29Z")

</div>

I think I’ve found a very rough ‘solution’. I’ve ensured the parent only rotates on the Y axis and also ‘fixed’ my quat math by converting it to Euler, doing the subtract, then back to quat. I realize I could run into Gimbal Lock here but it doesn’t seem an issue so far.
