I wanted to make a skill that can be used by both Player & Enemy.

How do I make it such that the skill will not be colliding with the owner/caster or colliding with both?

One stupid way I have is to create empty gameObjects as the Skill's child and set their tag as "targetPlayer","targetEnemy" or "targetAll" whenever my player/enemy cast the skills.

And when the skill collided with Player/Enemy, i will check the skill's children and see if there's matching tag and proceed if so.

Is there better way to do what i wanted??

You can put the skill object in a physics layer that collides only with players, enemies, or both. (Players & enemies each being in their own dedicated layer)

It's similar to your tag idea, except you don't need to check the tag inside the collision callback method. The callback won't even be called - the object will simply ignore collisions with objects it's not allowed to collide with according to the Collision Matrix you've configured.

If you don't want to do this layer-wide, you can also get a similar effect on a per-object basis using the Physics.IgnoreCollision() method to tell a particular pair of colliders to ignore each other (useful for a spellcaster to tell their own spell not to collide with themselves)

  • It sounds good for a single projectile. But what if I have something like a cluster bomb? The first object(bomb) will not damage the owner, but the children(baby bombs) will damage the owner because i can't ignoreCollision there. – RoyalCoconut May 18 at 2:19
  • You definitely can IgnoreCollision there. Just have the bomb remember who it's ignoring collisions with and pass on that knowledge to its babies. – DMGregory Aug 10 at 2:24

How about, when someone casts the skill, give a reference of the caster to it. Something like

caster.CastSkill(caster);

Then save caster inside the skill, and any time it collides with any other object, check if that object is the same as caster.

Alternatively, if you have a skill that has a specific target (for example) you can pass that target(s) and save them in the skill, so it can only collide with these targets.

This obviously is skill-dependent. Some skills may not care who is the caster, some skills may only differentiate between ally/enemy (in which case @DMGregory's answer is better).

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.