# Get index of collided instanced static mesh

**URL:** <https://forums.unrealengine.com/t/get-index-of-collided-instanced-static-mesh/466536>\
**Category:** World Creation\
**Tags:** unreal-engine, Instanced-Mesh, Physics, Collision, question\
**Created:** [June 10, 2020, 3:37pm UTC](https://forums.unrealengine.com/t/get-index-of-collided-instanced-static-mesh/466536 "2020-06-10T15:37:20Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThiloN1987](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ThiloN1987](https://forums.unrealengine.com/u/ThiloN1987)\
**Post date:** [June 10, 2020, 3:37pm UTC](https://forums.unrealengine.com/t/get-index-of-collided-instanced-static-mesh/466536/1 "2020-06-10T15:37:20Z")

</div>

Hello,

this question was often here and some claim to have found a solution, but they haven’t. If an object collided with an instanced static mesh component, the Hit Item in the Hit Result should give the index of the collided instanced static mesh. But it doesn’t, it’s always -1. What am I making wrong?

Thank you,

Thilo

---

<div class="post-metadata">

**Author:** ![Rene\_Zwaan](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Rene\_Zwaan](https://forums.unrealengine.com/u/Rene_Zwaan)\
**Post date:** [June 10, 2020, 3:57pm UTC](https://forums.unrealengine.com/t/get-index-of-collided-instanced-static-mesh/466536/2 "2020-06-10T15:57:32Z")

</div>

Hey ThiloN1987,

This seems to work for me.

 ![304083-hit.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/d/5/2d5a6eac8dd796a01930577d3f3adc9486537ebc.png)

---

<div class="post-metadata">

**Author:** ![Rene\_Zwaan](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Rene\_Zwaan](https://forums.unrealengine.com/u/Rene_Zwaan)\
**Post date:** [June 10, 2020, 4:00pm UTC](https://forums.unrealengine.com/t/get-index-of-collided-instanced-static-mesh/466536/3 "2020-06-10T16:00:33Z")

</div>

It gives me -1 if I am hitting something that is not part of the instance static mesh.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [June 10, 2020, 4:05pm UTC](https://forums.unrealengine.com/t/get-index-of-collided-instanced-static-mesh/466536/4 "2020-06-10T16:05:48Z")

</div>

Which is correct, btw.

---

<div class="post-metadata">

**Author:** ![ThiloN1987](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ThiloN1987](https://forums.unrealengine.com/u/ThiloN1987)\
**Post date:** [June 10, 2020, 4:09pm UTC](https://forums.unrealengine.com/t/get-index-of-collided-instanced-static-mesh/466536/5 "2020-06-10T16:09:57Z")

</div>

Thank you for your answer. I want to decide what happens with the instanced static mesh in the OnComponentHit-Event of the instanced static mesh component (e.g. a wall consisting of many bricks), not in the OnComponentHit-Event of the other colliding object (e.g. a gun projectile). As I found out, it works if the other component (gun projectile) is a non-physics-object. If it’s simulating physics, the OnComponentHit-Event of the gun projectile has the correct HitItem, but in the OnComponentHit-Event of the wall the HitItem is still -1.

---

<div class="post-metadata">

**Author:** ![ThiloN1987](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ThiloN1987](https://forums.unrealengine.com/u/ThiloN1987)\
**Post date:** [June 10, 2020, 4:12pm UTC](https://forums.unrealengine.com/t/get-index-of-collided-instanced-static-mesh/466536/6 "2020-06-10T16:12:45Z")

</div>

Look at this

---

<div class="post-metadata">

**Author:** ![ThiloN1987](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ThiloN1987](https://forums.unrealengine.com/u/ThiloN1987)\
**Post date:** [June 10, 2020, 4:28pm UTC](https://forums.unrealengine.com/t/get-index-of-collided-instanced-static-mesh/466536/7 "2020-06-10T16:28:15Z")

</div>

It’s bad engine design, I guess. If both colliding actors would return the right index, what if both actors are instanced static meshes? The indices would be different. There should be two indices for both colliding instanced static meshes.  
Now I have the projectile let the wall tell which of it’s brick it hit, because the wall doesn’t know it.

---

<div class="post-metadata">

**Author:** ![Rene\_Zwaan](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Rene\_Zwaan](https://forums.unrealengine.com/u/Rene_Zwaan)\
**Post date:** [June 10, 2020, 4:41pm UTC](https://forums.unrealengine.com/t/get-index-of-collided-instanced-static-mesh/466536/8 "2020-06-10T16:41:44Z")

</div>

Ah, I understand your problem. I had a similar problem before but with skeletal meshes. But it seems that hitresult mainly gives you information about the other object that you hit.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [June 10, 2020, 4:43pm UTC](https://forums.unrealengine.com/t/get-index-of-collided-instanced-static-mesh/466536/9 "2020-06-10T16:43:18Z")

</div>

The whole point of (hierarchical) instanced static meshes is to be extremely light and performant, they’re not supposed to simulate physics. Their transforms sit in the GPU cache and the CPU does the physics (to simplify it by an order of magnitude!). Update instances’ transforms (even via a batch mode) the benefits are mostly lost at this point.

You can move the component that holds onto the instances free of charge, though, so to speak.

* * *

When you collide with the instance, as demonstrated above, get its transform by the provided index, remove and replace it with a physics actor - feed it the transform, give it an impulse bump in the correct direction.

Making games is about cutting corners and faking stuff a lot of time, especially as things grow in scope.

> It’s bad engine design

We can do this at 120fps:

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/8/0/b80992fbfab2381767a74fa915c0c939eeaa95b6.jpeg "UE4 Optimization: Instancing") ](https://www.youtube.com/watch?v=oMIbV2rQO4k&t=126)

Not too shabby.

---

<div class="post-metadata">

**Author:** ![ThiloN1987](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ThiloN1987](https://forums.unrealengine.com/u/ThiloN1987)\
**Post date:** [June 10, 2020, 4:53pm UTC](https://forums.unrealengine.com/t/get-index-of-collided-instanced-static-mesh/466536/10 "2020-06-10T16:53:51Z")

</div>

This was **exactly** what I wanted to do. A wall consists of many bricks, so for it an instanced static mesh could be used. Then a gun projectile hit’s a specific brick and the brick breaks. Get the index of the instanced static mesh of the brick, remove it, replace it by a physics actor at the same transform, let it fall down. The problem is that my wall should decide what to do with a brick which was hit by a gun projectile. So the OnComponentHit-event of the wall respectively of the instanced static mesh component should know the index of the instanced static which was hit by the projectile, not the projectile. **But it’s like I said, the projectile knows the correct index, but the wall receives index -1. This is only true if the projectile is simulating physics. If it’s using projectile movement component, both have the right index.**

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [June 10, 2020, 4:59pm UTC](https://forums.unrealengine.com/t/get-index-of-collided-instanced-static-mesh/466536/11 "2020-06-10T16:59:37Z")

</div>

> The problem is that my wall should  
> decide what to do with a brick which  
> was hit by a gun projectile.
> 
> But it’s like I said, the projectile  
> knows the correct index

What exactly is stopping you from propagating the data from the projectile to the wall actor?

---

<div class="post-metadata">

**Author:** ![ThiloN1987](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ThiloN1987](https://forums.unrealengine.com/u/ThiloN1987)\
**Post date:** [June 10, 2020, 5:00pm UTC](https://forums.unrealengine.com/t/get-index-of-collided-instanced-static-mesh/466536/12 "2020-06-10T17:00:06Z")

</div>

I can solve this by making an (additional) line trace through the wall:

 ![304093-lt.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/3/f/b3f538b782a44e78b2154e9aab4baf55c549f30e.jpeg)

But this is not efficient as the projectile already knows the index of the instanced static mesh and perhaps I will need an additional trace channel for this…

---

<div class="post-metadata">

**Author:** ![ThiloN1987](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ThiloN1987](https://forums.unrealengine.com/u/ThiloN1987)\
**Post date:** [June 10, 2020, 5:11pm UTC](https://forums.unrealengine.com/t/get-index-of-collided-instanced-static-mesh/466536/13 "2020-06-10T17:11:43Z")

</div>

Seems not to be very safe to me. I could just let the projectile tell the wall that it has hit it and ignore the OnComponentHit of the instanced static mesh component, but probably the wall needs the information in the HitResult of it’s instanced static mesh, too. So I probably need both informations. So if the OnComponentHit of the wall is fired, **wait for the information the projectile tells it** … How to do this? Also, what if another collision with the same projectile occures before the wall receives the information from the projectile? And what if multiple projectiles are hitting the wall and sending the information? It just seems not to be safe to me, I would expect difficult to resolve bugs with it as they have to do with asynchronously.
