- Home /
Changing Collider Position for Different Animation Clips
Hello, I am currently making a 2D topdown game. I have a character with a cone of vision- I've done this by adding a child object to the character and then putting an ontrigger collider on the child. I've made four animation clips for going up, down, left and right. I've managed to get the sprites to change for the character and cone of sight just as I want them to. However, I'm having difficulties with changing the position of the collider to match each animation clip. As you can see in the picture, when the down animation clip is running, the position of the collider does not match. This is the case for the other animation clips too.
I've tried to overcome this problem by adding 4 polygon colliders (in the positions that I want them in) to the sight gameobject and enabling them depending on the direction that the character is travelling:
public Collider2D upCollider;
public Collider2D downCollider;
public Collider2D leftCollider;
public Collider2D rightCollider;
Vector2 dir;
void Start()
{
dir = GetComponentInParent<RootController>().direction;
}
void Update()
{
whichCollider();
}
void whichCollider()
{
if(dir == -Vector2.down)
{
upCollider.enabled = true;
downCollider.enabled = false;
rightCollider.enabled = false;
leftCollider.enabled = false;
}
else if(dir == Vector2.down)
{
downCollider.enabled = true;
upCollider.enabled = false;
leftCollider.enabled = false;
rightCollider.enabled = false;
}
else if(dir == -Vector2.right)
{
leftCollider.enabled = true;
rightCollider.enabled = false;
upCollider.enabled = false;
downCollider.enabled = false;
}
else if(dir == Vector2.right)
{
rightCollider.enabled = true;
leftCollider.enabled = false;
upCollider.enabled = false;
downCollider.enabled = false;
}
}
But this does not help, it only enables one polygon collider and continues the game with that same collider for all 4 animations. Any help would be highly appreciated. Thank you.