I am writing a simple towerDefence game.
There are a lot of ghost mobs and rockets, both spawning and flying around.
Ghost mobs sometimes turn from spirits to fleshy and conversely.
I want rockets to move through them in spirit mode and collide when they are fleshy.
I thought it can be achieved via collision layers, but it seems that they cannot be dynamically changed through scripting.
Now i am just searching for elegant solution, that dont suggest counting every rocket and making IgnoreCollision on them for each mob.
Layers are the best solution. You don’t need to redefine the layer matrix at runtime - simply change the mob’s layer from default to a specially created ghost layer depending on its state, and let the missiles in a missile layer, which doesn’t collide with ghost. Supposing that you’ve created the ghost layer (menu Edit/Project Settings/Tags) in layer 8, control the mob’s layer like this:
if (ghostMode){
gameObject.layer = 8; // move to ghost layer
} else {
gameObject.layer = 0; // move to regular layer
}
NOTE: Remember to create also the missile layer, and to configure the Collision Matrix in the Physics Manager (menu Edit/Project Settings/Physics) so that objects in ghost don’t collide with missile.
I am no expert, but what about replacing them on the change event?
Afaik all object can be deleted and replaced with another object, which can inherit values from the former (like position, health and so on…)
This way it would certainly be possible to have different impact or collision events.