# How to get the keypoint 2D and 3D of persons, to spawn an object on that keypoint?

**URL:** <https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531>\
**Category:** C#\
**Tags:** unity\
**Created:** [September 20, 2023, 12:44pm UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531 "2023-09-20T12:44:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rachael](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Rachael](https://community.stereolabs.com/u/Rachael)\
**Post date:** [September 20, 2023, 12:44pm UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/1 "2023-09-20T12:44:55Z")

</div>

how to get the keypoint 2D and 3D of persons, to spawn an object on that keypoint?

---

<div class="post-metadata">

**Author:** ![BenjaminV](https://community.stereolabs.com/user_avatar/community.stereolabs.com/benjaminv/32/3863_2.png) [@BenjaminV](https://community.stereolabs.com/u/BenjaminV)\
**Post date:** [September 20, 2023, 12:55pm UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/2 "2023-09-20T12:55:29Z")

</div>

Hi,

The data is stored in a **sl.BodyData** structure. You can look at how we access it in our body tracking sample, here : [https://github.com/stereolabs/zed-unity/blob/master/ZEDCamera/Assets/ZED/SDK/Helpers/Scripts/BodyTracking/ZEDBodyTrackingManager.cs#L341](https://github.com/stereolabs/zed-unity/blob/master/ZEDCamera/Assets/ZED/SDK/Helpers/Scripts/BodyTracking/ZEDBodyTrackingManager.cs#L341)

When you subscribe to the **OnBodyTracking** event ([https://github.com/stereolabs/zed-unity/blob/master/ZEDCamera/Assets/ZED/SDK/Helpers/Scripts/BodyTracking/ZEDBodyTrackingManager.cs#L181](https://github.com/stereolabs/zed-unity/blob/master/ZEDCamera/Assets/ZED/SDK/Helpers/Scripts/BodyTracking/ZEDBodyTrackingManager.cs#L181)), you can have access to all the data from that frame as a **BodyTrackingFrame** which contains a list of **BodyData**.

---

<div class="post-metadata">

**Author:** ![Rachael](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Rachael](https://community.stereolabs.com/u/Rachael)\
**Post date:** [September 20, 2023, 1:03pm UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/3 "2023-09-20T13:03:44Z")

</div>

thanks - reading through all this now

---

<div class="post-metadata">

**Author:** ![Rachael](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Rachael](https://community.stereolabs.com/u/Rachael)\
**Post date:** [September 20, 2023, 3:34pm UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/4 "2023-09-20T15:34:06Z")

</div>

Hi there,

I think it’s almost at the location, where it’s suppose to be spawning but not quite.  
The necklace needs to be facing the opposite direction and it needs to be sitting on node 4.  
The necklace is only suppose to be spawning once per human body detected.

Based on the image and explanation, would you be able to guide me…

public void Start(){

```
        if (spawned == false)
        {
            var zedManager = ZEDManager.GetInstance(ZED_CAMERA_ID.CAMERA_ID_01);
            zedManager.OnBodyTracking += OnBodyTracking;

        }

    }

private void OnBodyTracking(BodyTrackingFrame bodyFrame)
        {
        bodyFrame.detectedBodies.ForEach(body =>
        {
            spawned = true;
            var neckPosition = body.rawBodyData.localPositionPerJoint[(int)BODY_38_PARTS.NECK];
            var neckRotation = body.rawBodyData.localOrientationPerJoint[(int)BODY_38_PARTS.NECK];

            Instantiate(necklace, neckPosition, neckRotation);}

        });
    }
}

```

 ![image](https://community.stereolabs.com/uploads/default/original/2X/9/9b5ec76f83b4bc0e7a7393323efa080260810555.jpeg)

---

<div class="post-metadata">

**Author:** ![BenjaminV](https://community.stereolabs.com/user_avatar/community.stereolabs.com/benjaminv/32/3863_2.png) [@BenjaminV](https://community.stereolabs.com/u/BenjaminV)\
**Post date:** [September 20, 2023, 3:39pm UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/5 "2023-09-20T15:39:38Z")

</div>

Can you try :

`var neckPosition = zedManager.GetZedRootTansform().TransformPoint(body.rawBodyData.keypoint[(int)BODY_38_PARTS.NECK]);` instead ?

You are getting the local position, not the world position of the neck.

Stereolabs Support

---

<div class="post-metadata">

**Author:** ![Rachael](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Rachael](https://community.stereolabs.com/u/Rachael)\
**Post date:** [September 20, 2023, 4:23pm UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/6 "2023-09-20T16:23:40Z")

</div>

😃 Ok, i’ll try that out and test it - thank you so much.  
I’ll let you know how it goes

---

<div class="post-metadata">

**Author:** ![Rachael](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Rachael](https://community.stereolabs.com/u/Rachael)\
**Post date:** [September 20, 2023, 4:52pm UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/7 "2023-09-20T16:52:48Z")

</div>

Hi Benjaminv,

So, i tried what you suggested and applied other changes to my prefab.  
Things are working.

I have one last question, though.

This image renders through the camera.  
As you can see, the necklace isnt so shiny.

 ![image](https://community.stereolabs.com/uploads/default/original/2X/8/86e91e00cb74def87ab3a7dfe7bdf21482ed0089.jpeg)

But the prefab itself, is quite shiny.

 ![image](https://community.stereolabs.com/uploads/default/original/2X/9/90ae90bef334e4d02af1ef5e2395d3f4429652ab.jpeg)

How do i get the shiny necklace to remain shiny within the image rendered through the ZEDCamera, as i see it trips the shininess away from the rendered image…

Thanks

---

<div class="post-metadata">

**Author:** ![Rachael](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Rachael](https://community.stereolabs.com/u/Rachael)\
**Post date:** [September 20, 2023, 5:20pm UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/8 "2023-09-20T17:20:46Z")

</div>

I also have it that the object around the neck, being the necklace is spawning multiple times infinitely.  
Do you know any solution that you can recommend, where the necklaces only instantiate once per detected body?

my work around is… see below:

* * *

public class DetectKeypointsOnHuman : MonoBehaviour  
{  
[SerializeField] public GameObject necklace;  
private GameObject necklaceSpawning;  
private bool spawned = false;

```
private List<GameObject> _spawnedObjects = new List<GameObject>();

    // Update is called once per frame
    public void Start()
    { if (spawned == false)
        {
            var zedManager = ZEDManager.GetInstance(ZED_CAMERA_ID.CAMERA_ID_01);
            zedManager.OnBodyTracking += OnBodyTracking;

        }

    }

    private void OnBodyTracking(BodyTrackingFrame bodyFrame)
    {
        _spawnedObjects.ForEach(x =>
        {
            Destroy(x);
        });
        _spawnedObjects.Clear();

        bodyFrame.detectedBodies.ForEach(body =>
        {
            spawned = true;
            var zedManager = ZEDManager.GetInstance(ZED_CAMERA_ID.CAMERA_ID_01);
            var neckPosition = zedManager.GetZedRootTansform().TransformPoint(body.rawBodyData.keypoint[(int)BODY_38_PARTS.NECK]);
            var neckRotation = body.rawBodyData.localOrientationPerJoint[(int)BODY_38_PARTS.NECK];

            _spawnedObjects.Add(Instantiate(necklace, neckPosition, neckRotation));

        });
    }
}

```

---

<div class="post-metadata">

**Author:** ![BenjaminV](https://community.stereolabs.com/user_avatar/community.stereolabs.com/benjaminv/32/3863_2.png) [@BenjaminV](https://community.stereolabs.com/u/BenjaminV)\
**Post date:** [September 21, 2023, 7:05am UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/9 "2023-09-21T07:05:50Z")

</div>

For the first question, can you try to disable the “AR Post Processing” option, available on the ZED Manager script, in the **AR PassThrough Settings** section.

You are indeed spawning the necklace each frame. You need to instantiate it once for each detection. For that, you can use the detection **id**.

We are doing something similar to spawn only one avatar per detection. You can take a look at the **UpdateSkeletonData** function.

Stereolabs Support

---

<div class="post-metadata">

**Author:** ![Rachael](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Rachael](https://community.stereolabs.com/u/Rachael)\
**Post date:** [September 21, 2023, 8:02am UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/10 "2023-09-21T08:02:56Z")

</div>

Thanks Benjamin, for the reply.

I’m going to go through all your feedback soon & i’ll let you know how it goes.

---

<div class="post-metadata">

**Author:** ![Rachael](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Rachael](https://community.stereolabs.com/u/Rachael)\
**Post date:** [September 21, 2023, 8:49am UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/11 "2023-09-21T08:49:09Z")

</div>

First part - done - feedback: Looks better but not as shiny, in it’s original shiny state but it’s okay for now.

Working on the next part - will let you know how that goes.

And thanks for the help.  
I’m learning so much!

---

<div class="post-metadata">

**Author:** ![Rachael](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Rachael](https://community.stereolabs.com/u/Rachael)\
**Post date:** [September 25, 2023, 7:57pm UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/12 "2023-09-25T19:57:22Z")

</div>

Hi

I got my game object to spawn/instantiate only once instead of multiple times, around the neck area.

```auto
 public void Start()
        {
            zedManager = ZEDManager.GetInstance(ZED_CAMERA_ID.CAMERA_ID_01);
            zedManager.OnBodyTracking += BodyTracking;
        }

        private void BodyTracking(BodyTrackingFrame bodyFrame)
        {
            var missingBodies = currentlyTrackedBodies.Where(existingBodies => !bodyFrame.detectedBodies.Select(detectedBodies => detectedBodies.id).ToList().Contains(existingBodies.Key)).ToList();

            missingBodies.ForEach(existingBody =>
            {
                Destroy(existingBody.Value);
                currentlyTrackedBodies.Remove(existingBody);
            });

            bodyFrame.detectedBodies.ForEach(body =>
            {
                int person_id = body.rawBodyData.id;

                var position = zedManager.GetZedRootTansform().TransformPoint(body.rawBodyData.keypoint[(int)BODY_38_PARTS.NECK]);
                var rotation = body.rawBodyData.localOrientationPerJoint[(int)BODY_38_PARTS.NECK];

                //////// THIS ROTATION MIGHT NEED TO BE CHANGED
                rotation = new Quaternion(rotation.x,
                                            rotation.y,
                                            rotation.z * -1.0f,
                                            rotation.w * -1.0f);

                if (!currentlyTrackedBodies.ContainsKey(person_id))
                {
                    var necklace = Instantiate(necklacePrefab, position, rotation);
                    currentlyTrackedBodies.Add(person_id, necklace);
                }
                else
                {
                    var necklace = currentlyTrackedBodies.SingleOrDefault(x => x.Key == person_id).Value;
                    necklace.transform.position = Vector3.Lerp(necklace.transform.position, position, Time.deltaTime * 100);
                    necklace.transform.rotation = Quaternion.Lerp(necklace.transform.rotation, rotation, Time.deltaTime * 100);
                }
            });
        }

```

---

<div class="post-metadata">

**Author:** ![Rachael](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Rachael](https://community.stereolabs.com/u/Rachael)\
**Post date:** [September 25, 2023, 8:00pm UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/13 "2023-09-25T20:00:02Z")

</div>

I have another question.

How do i get the global rotation on the neck keypoint from the human body.

You have given me the global position and that worked lovely, would you be able to provide me with the code to get the global rotation, as i’ve tried a few things but none seems to work.

current code snippet:

```
            var position = zedManager.GetZedRootTansform().TransformPoint(body.rawBodyData.keypoint[(int)BODY_38_PARTS.NECK]);
            var rotation = body.rawBodyData.localOrientationPerJoint[(int)BODY_38_PARTS.NECK];

```

My rotation is happening in reverse to how i move my neck.

---

<div class="post-metadata">

**Author:** ![Rachael](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Rachael](https://community.stereolabs.com/u/Rachael)\
**Post date:** [September 25, 2023, 8:36pm UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/14 "2023-09-25T20:36:39Z")

</div>

![image](https://community.stereolabs.com/uploads/default/original/2X/5/5ca4e379c41bba962d12c3ff6c0007947ac26ae9.jpeg)  
 ![image](https://community.stereolabs.com/uploads/default/original/2X/1/1d6d1393b22048f5c648884e9859a7442417c32d.jpeg)  
 ![image](https://community.stereolabs.com/uploads/default/original/2X/5/5c6c6eb8440467ded74e05d78171bb3af7088e08.jpeg)

See how it’s not following my head… it’s going in reverse and not mirrored to the rotation of my neck.

---

<div class="post-metadata">

**Author:** ![Rachael](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Rachael](https://community.stereolabs.com/u/Rachael)\
**Post date:** [September 25, 2023, 8:39pm UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/15 "2023-09-25T20:39:53Z")

</div>

Also, i’ve disabled the AR post processing but the necklace is not as shiny, as the mesh that sits within our game object. Is there something else, I can do to enhance the shininess, please?

---

<div class="post-metadata">

**Author:** ![BenjaminV](https://community.stereolabs.com/user_avatar/community.stereolabs.com/benjaminv/32/3863_2.png) [@BenjaminV](https://community.stereolabs.com/u/BenjaminV)\
**Post date:** [September 26, 2023, 7:17am UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/16 "2023-09-26T07:17:05Z")

</div>

Hi,

To get the global rotation of the neck, you need to add up all the rotations of the neck and all of its parents, up to the root.

There is actually another parameter you can disable in the ZED Manager, it’s called “Grey out Skybox at Start”. On top of that, you can play with the lights in the scene to make it more shiny.

Stereolabs Support

---

<div class="post-metadata">

**Author:** ![Rachael](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Rachael](https://community.stereolabs.com/u/Rachael)\
**Post date:** [September 26, 2023, 8:09am UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/17 "2023-09-26T08:09:57Z")

</div>

Ok, trying this out - thanks!

---

<div class="post-metadata">

**Author:** ![Rachael](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Rachael](https://community.stereolabs.com/u/Rachael)\
**Post date:** [September 26, 2023, 11:20am UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/18 "2023-09-26T11:20:02Z")

</div>

Hi @BenjaminV

Could you give me an example of something to reference, for the below statement:  
“To get the global rotation of the neck, you need to add up all the rotations of the neck and all of its parents, up to the root.”

Struggling to get my head wrapped around this statement.

---

<div class="post-metadata">

**Author:** ![BenjaminV](https://community.stereolabs.com/user_avatar/community.stereolabs.com/benjaminv/32/3863_2.png) [@BenjaminV](https://community.stereolabs.com/u/BenjaminV)\
**Post date:** [September 26, 2023, 11:45am UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/19 "2023-09-26T11:45:41Z")

</div>

A local rotation is the rotation of a joint relative to its parent. So, to have the global rotation, you need to not only apply the local rotation of the neck and also the local rotation of its parent, and the local rotation of its parent, until applying the rotation of the root (which is the pelvis keypoint for the sdk).

Actually, maybe there is another solution. If you set the necklace game object as the child of the neck game object in the hierarchy (on the game object of the avatar), it should automatically follow the neck’s rotation.

Stereolabs Support

---

<div class="post-metadata">

**Author:** ![Rachael](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Rachael](https://community.stereolabs.com/u/Rachael)\
**Post date:** [September 26, 2023, 1:04pm UTC](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531/20 "2023-09-26T13:04:21Z")

</div>

Ok, the issue was not code related or how i set up my game object but how very lit the room was & all the reflective surfaces from behind me (i.e. the snake tanks).

Issue sorted, on having the game object rotate with the users neck

[Next page](https://community.stereolabs.com/t/how-to-get-the-keypoint-2d-and-3d-of-persons-to-spawn-an-object-on-that-keypoint/3531.md?page=2)
