I am using a custom platformer physics script from a tutorial
https://learn.unity.com/tutorial/live-session-2d-platformer-character-controller
For my game, the player has a melee attack, and I figured I could do damage by having a collider set as a trigger that is enabled when the player is attacking, and disabled when not attacking.
The script uses a RigidBody2D.Cast to detect how the player is colliding with the environment.
The following code sets up the contactFilter that will be used by this cast
protected ContactFilter2D contactFilter;
void Start()
{
contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(0));
contactFilter.useLayerMask = true;
contactFilter.useTriggers = false;
}
I assumed that "contactFilter.useTriggers = false;" would make it so that the melee collider that is a trigger will not be used in the cast.
However, it seems to be used anyway, since when I attack with the player, he has the collider as a hitbox that interacts with the environment.
How can I make it so that the trigger collider is not used in this cast?
also, here is where the cast happens
void Movement(Vector2 move, bool yMovement)
{
float distance = move.magnitude;
if (distance > minMoveDistance)
{
int count = rb2d.Cast(move, contactFilter, hitBuffer, distance + shellRadius);
hitBufferList.Clear();
for (int i = 0; i < count; i++)
{
hitBufferList.Add(hitBuffer[i]);
}
for (int i = 0; i < hitBufferList.Count; i++)
{
Vector2 currentNormal = hitBufferList[i].normal;
if (currentNormal.y > minGroundNormalY)
{
grounded = true;
if (yMovement)
{
groundNormal = currentNormal;
currentNormal.x = 0;
}
}
float projection = Vector2.Dot(velocity, currentNormal);
if (projection < 0)
{
velocity = velocity - projection * currentNormal;
}
float modifiedDistance = hitBufferList[i].distance - shellRadius;
distance = modifiedDistance < distance ? modifiedDistance : distance;
}
}
rb2d.position = rb2d.position + move.normalized * distance;
}
↧