How do I make a Projectile collide or not with Character layer based on the character's team

Hi,

I have a projectile that must collide or skip collission with an object on the character layer based on if its friendly or not. I have a script called "Team" and I can easily check what team the object I'm colliding with is, but the problem is that I can't make that object go through or collide with it, its just one or the other.

For example if I make it collide then it will always collide no matter what the team is even if I do

protected override void OnCollision(RaycastHit? hit)
{
if (hit.HasValue && hit.Value.collider.name.Equals("Collider"))
{
Debug.Log("Check");
return;
}

if (m_ScheduledDeactivation != null)
{
SchedulerBase.Cancel(m_ScheduledDeactivation);
m_ScheduledDeactivation = null;
}

base.OnCollision(hit);
}

The projectile is already "stuck" on the object when that OnCollission gets called. So I figured I should make it go through every object on the Character layer, but I need a way to make it collide and deal damage if its an enemy. Is there something like "OnPreCollission"?

EDIT: If I can somehow edit the last few lines of TrajectoryObject.cs's Cast method and add

Code:
if (hitCount > 0)
{
    m_RaycastHit = m_CombinedRaycastHits[index];
    
// TODO : Validate m_RaycastHit's value



    return true;
}
return false;


Is there a way to do that without editing your code?
 
Last edited:
inheriting from Projectile and then doing this works so far

C#:
     protected override bool Cast(Vector3 position, Quaternion rotation, Vector3 direction)
        {
            var result = base.Cast(position, rotation, direction);
            
            if (result)
            {
                if (m_RaycastHit.collider.gameObject.TryGetComponent<TeamMember>(out var hitObjectTeamMember))
                {
                    if (_myTeamMember.TeamId.Equals(hitObjectTeamMember.TeamId))
                    {
                        return false;
                    }
                }
            }

            return result;
        }
 
Does changing the impact layers not work? Have it only impact enemy and whatever else but not character layer

It works, but I wanted to have all enemies and player on the same level because Im switching them from friendly to enemy at runtime and I did not want to switch layers at runtime.
 
Top