Hi. I'm working on a project right now and the main ability of the player is being able to grow and shrink whenever. I have no issue doing this BUT... if I grow while next to a wall my guy will go through the wall. I've tried using a raycast but I have objects in my game that have trigger colliders on them and the raycast detects the trigger. I also tried making the raycast ignore the trigger but it's attached to an object I want the player to collide with. So, if i make the raycast ignore that layer it will ignore the mesh I want the player to interact with... Please help. Thanks.
asked Sep 22 '10 at 06:47 PM
Your situation is such that you have some object (player or whatever) that can be scaled at runtime. When next to objects, this causes penetration as scaling will properly resize the collider of this object, but will not generate collisions. The only way I know of to resolve this is to perform a raycast and offset.
You seem to be confused about raycasts or when you're doing them. You have objects with triggers colliders and objects with regular colliders, but that really doesn't mean anything to your problem, so consider them all as colliders just the same. To restate your problem in simpler terms, you have a number of objects with colliders and you only want to raycast against some of them. This can be done either with layers or on an object by object case.
I'm sure somewhere in the 90+ questions tagged raycast, there's an answer to this, but I don't blame you for not finding it.
To raycast only against certain layers, you would supply a layermask. Different raycasts can use different layermasks. Different objects (even children and parents) can be on different layers. If you simply place the things you don't want to cast against in certain raycasts on different layers than those you wish to cast against in those raycasts, you'll be fine.
Say for example, I have a walls with colliders on Layer 9 (collision geo) and sound trigger colliders on Layer 10 (sound zones) and desks with colliders on layer 11 (interactive collision geo) and interactive zones for the desks on Layer 12 (interaction zones). If I want to shoot a gun , but not go through the walls or desks, I could raycast to those objects I wouldn't want to pass through, but not the interactive zones. So I would create a layer mask.
If this is not quite what you want, you may consider raycasting to the objects of importance individually, but this is a bit more expensive. You'll likely want to tag your objects for this purpose and at the very least only cast against objects within a set radius.
One thing to consider is that any offset you apply will upset the results from your raycasts because you will have just moved and you would then need to cast again to ensure that your offset didn't put you through a different object. This is where you would need to design your world and player abilities to prevent problems where you have a character being scaled into the walls on opposite sides.
Remember only to do your scaling raycasting when you're scaling up (and maybe down, depending on your use case).
answered Sep 22 '10 at 08:02 PM