Download

How to 'Hide' an object from Line Trace after picking it up? (C++ code)

Hi all.

I have written this function for my pickup-able object, in order that it can be put into the inventory and hidden away from the game (I want to keep it alive so they can place it back down somewhere else).

So really all I want is the item to be invisible and have no collision or lighting or any other effect on the game whilst it is hidden. My function seems to work, except the Line Trace still finds it (so I guess the collision box remains in place even when ‘Hidden’.) One idea I have is to just move the object to a temp location such as -9999,-9999,-9999, but really I just need to deactivate the collision or the line trace channel temporarily.

Here is my code so far (if you need to see all the code please let me know):

void UPickup_Object::HideFromGameWorld(bool b)
{
	if(GetOwner()->FindComponentByClass<UStaticMeshComponent>() != nullptr)
	{
		GetOwner()->FindComponentByClass<UStaticMeshComponent>()->bHiddenInGame = b;
		GetOwner()->FindComponentByClass<UStaticMeshComponent>()->SetVisibility(!b, true);
	}
}

My bad I think I found the answer already. Here is my function now and it does seem to work the way I wanted:

void UPickup_Object::HideFromGameWorld(bool b)
{
	if(GetOwner()->FindComponentByClass<UStaticMeshComponent>() != nullptr)
	{
		GetOwner()->FindComponentByClass<UStaticMeshComponent>()->bHiddenInGame = b;
		GetOwner()->FindComponentByClass<UStaticMeshComponent>()->SetVisibility(!b, true);
	}

	if(GetOwner()->FindComponentByClass<UPrimitiveComponent>() != nullptr)
	{
		GetOwner()->FindComponentByClass<UPrimitiveComponent>()->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
	}
}

Of course, I will need to rework the function a little, because if I try to un-Hide an object using this function, the CollisionResponse will never get set back to its original case.

1 Like

Any reason you’re handling the pickup like this and not by destroying the actor on pickup and creating a new one when the inventory item is placed back down again?