How to change static mesh when you're looking at it?

Hello all,

I’m trying to change an object to a different object when you look at it.

For example:

When I look at Cube1 and press “E” it changes to a Table. And when I look at Cube 2 and press “E” it changes to a Chair. I don’t know how to set the GetWorldLocation at the Target parameter of Set Static Mesh function.

Does anyone know how I can make it work? I need your advice. :slight_smile:

This is currently what is working, but not the way I want it. Because whenever I press “E” both objects changes the same time unfortunately. But I only want an object to change when I look at it.

Don’t mind the bad radio model on the left side haha

Hey Chris,

when you press E both StaticMeshes change because you implemented it like this.

I would recommend that you make a LineTrace and
First:
Make 2 bool Variables. TargetCube1WasHit and TargetCube2WasHit
Then make your Line Trace
Check if the HitComponent is either your TargetCube1 or TargetCube2. If its TargetCube1 set your TargetCube1WasHit to true.
Don’t forget to set the opposite bool Variable to false. In this case TargetCube2WasHit.

Then whenever you press E check if TargetCube1WasHit or TargetCube2WasHit is true.
Of course only one should be true or both false.

Now you can set the Static Mesh :slight_smile:

There are more elegant ways but this is the fastest I could think of now :smiley:

Good Luck :wink:

Thank you for your answer !

I can indeed do that. But if I had 20 objects then I had to make 20 variables. So that’s why I wanted to GetObjectLocation if that exists and is possible haha. It would be great if my LineTrace could point at a object, and can change to a different object when I press “E”.

But thank you for your help, I really appreciate it! It gave me some ideas which might solve this problem. :slight_smile:

Chris, instead of creating a different variable for every item that could be changed (as described by ), you could just create a single variable called “CurrentGazeObject” that is a reference to whatever object was the HitActor result of the most recent line trace (or null if no object is currently being gazed at.) Then you’d just perform a SetStaticMesh using the object reference held in that variable.

In cases like this I also find it helpful to add an Actor tag to each of the objects in your scene that are allowed to be changed in this way so that your logic can check for that tag (using the ActorHasTag node) before making the swap.

Hey Krytopher,

I had the same idea. The problem is that every StaticMeshComponent has another StaticMesh which represents it.
TargetCube1 becomes Radio1
TargetCube2 becomes Radio2

Maybe combining this idea with a TSet which stores a reference to the StaticMeshComponent as a key and it’s StaticMesh as a Value.