# Unable to properly interpolate an event in 3D space

**URL:** <https://qa.fmod.com/t/unable-to-properly-interpolate-an-event-in-3d-space/18687>\
**Category:** Unity\
**Tags:** unity\
**Created:** [April 21, 2022, 3:26pm UTC](https://qa.fmod.com/t/unable-to-properly-interpolate-an-event-in-3d-space/18687 "2022-04-21T15:26:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![g.hadji](https://avatars.discourse-cdn.com/v4/letter/g/b19c9b/32.png) [@g.hadji](https://qa.fmod.com/u/g.hadji)\
**Post date:** [April 21, 2022, 3:26pm UTC](https://qa.fmod.com/t/unable-to-properly-interpolate-an-event-in-3d-space/18687/1 "2022-04-21T15:26:10Z")

</div>

Hi. I’ve been stuck for hours trying to attach my fmod Event to a moving object’s position. The idea is to hear a projectile passing by when the player is close to the trajectory. Here’s my code :

```auto
    public void PiratePatternAudioObjectCreate()
    {
      m_shootFinalTarget = new GameObject("shootFinalTargetLocation");
      m_lerpingObject = new GameObject("lerpingObject");
    }

    public void PiratePatternAudioObjectDestroy()
    {
      Destroy(m_shootFinalTarget);
      Destroy(m_lerpingObject);
    }

    public void PiratePatternShootObstacle(Vector3 shootFinalTarget)
    {
      if(m_shootObstacleTimer <= 1)
      {
        PiratePatternAudioObjectDestroy();
        PiratePatternAudioObjectCreate();

        m_shootFinalTarget.transform.position = shootFinalTarget;

        currentPos = m_boatController.Physics.transform.position;
        targetPos = m_shootFinalTarget.transform.position;

        RuntimeManager.PlayOneShotAttached(m_piratePatternShoot.Guid, m_boatController.Physics.gameObject);
        RuntimeManager.PlayOneShotAttached(m_piratePatternShootObstacleHit.Guid, m_shootFinalTarget);
        // RuntimeManager.PlayOneShotAttached(m_piratePatternShootWhoosh.Guid, m_lerpingObject);
         m_piratePatternShootWhooshInstance = RuntimeManager.CreateInstance(m_piratePatternShootWhoosh);

         m_piratePatternShootWhooshInstance.start();
         m_piratePatternShootWhooshInstance.release();
      }
      m_lerpingObject.transform.position = currentPos;

      float obstacleLenght = Vector3.Distance(m_boatController.Physics.transform.position, targetPos);
      float speed = 40f;
      m_shootObstacleTimer += 1;

      if(m_shootObstacleTimer > 1)
      {
        currentPos = Vector3.MoveTowards(currentPos, targetPos, speed * Time.deltaTime);
        RuntimeManager.AttachInstanceToGameObject(m_piratePatternShootWhooshInstance, m_lerpingObject);
        if(Vector3.Distance(m_lerpingObject.transform.position, targetPos) < 0.1)
          m_lerpingObject.transform.position = targetPos;
      }

```

As the projectile is triggered, my function PiratePatternShootObstacle is called. It creates an object “m\_lerpingObject” which is moving from the enemy to a hit point. What I want is to get my event to follow this object. In order to do so, I used the AttachInstanceToGameObject Function. While profiling, I can see that whenever the event is called, it fires really far away from the enemy, but in the same direction as my hit point, but I can’t figure out why.

 ![Capture](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/f/f0d1c71caa74e6fd89e6d659f18ee2e00840fcc9.png)  
 ![Capture2](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/2/23251a9a7bec470358cf6859f727a4a6c4f2823f.png)

---

<div class="post-metadata">

**Author:** ![g.hadji](https://avatars.discourse-cdn.com/v4/letter/g/b19c9b/32.png) [@g.hadji](https://qa.fmod.com/u/g.hadji)\
**Post date:** [April 22, 2022, 7:22am UTC](https://qa.fmod.com/t/unable-to-properly-interpolate-an-event-in-3d-space/18687/2 "2022-04-22T07:22:44Z")

</div>

So I slept on it and here is where I was wrong. I was creating my EventInstance in another condition that the one responsible for interpolating my gameObject. I don’t really know why, but trying to access the already created Instance from a different condition by using RuntimeManager.AttachInstanceToGameObject(instance, gameObject) doesn’t affect the Instance.  
In order to make it work I just has to create my instance in the same place where it is connected to my 3D gameObject position, making sure to limit the instance max number to 1(with no stealing) in fmod studio’s event editor since it is called every frame :

```auto
public void PiratePatternShootObstacle(Vector3 shootFinalTarget)
    {
      if(m_shootObstacleTimer <= 1)
      {
        PiratePatternAudioObjectDestroy();
        PiratePatternAudioObjectCreate();

        m_shootFinalTarget.transform.position = shootFinalTarget;
        m_lerpingObject.transform.position = m_boatController.Physics.transform.position;
        targetPos = m_shootFinalTarget.transform.position;

        RuntimeManager.PlayOneShotAttached(m_piratePatternShoot.Guid, m_boatController.Physics.gameObject);
        RuntimeManager.PlayOneShotAttached(m_piratePatternShootObstacleHit.Guid, m_shootFinalTarget);
      }
      // float obstacleLenght = Vector3.Distance(m_boatController.Physics.transform.position, targetPos);
      float speed = 40f;
      m_shootObstacleTimer += 1;

      if(m_shootObstacleTimer > 1)
      {
        m_lerpingObject.transform.position = Vector3.MoveTowards(m_lerpingObject.transform.position, targetPos, speed * Time.deltaTime);
        m_piratePatternShootWhooshInstance = RuntimeManager.CreateInstance(m_piratePatternShootWhoosh);
        RuntimeManager.AttachInstanceToGameObject(m_piratePatternShootWhooshInstance, m_lerpingObject.transform);
        m_piratePatternShootWhooshInstance.start();
        m_piratePatternShootWhooshInstance.release();
      }

```
