- Home /
How Can I switch between 2 colliders in my 2D Game?
Hey Guys!
I'm almost finish with my first 2D-Platformer. The last thing I want to do is to bend down my character that I don't collide with birds. I implemented the Graphics and can call it by pressing the down button on my keyboard. The problem is: The bird applies damage on me even if I'm bend down because I have a mesh collider on my player which touches the box collider of the bird.
I thought I could fix this if I make an if-request: if my player is bend down->Turn off my mesh collider and activate a new box collider, which is smaller, that other animals on the ground can also apply damage to me.
The problem is I have really no idea how to do this.
The control for my character is in the script "PlayerController" and the apply damage function is in may "WaypointWalker" script. It looks like this:
protected virtual void ApplyDamage (int damage)
{
if(!isHit)
{
isHit = true;
currentHealth -= damage;
AudioSource.PlayClipAtPoint(hitSound,transform.position,1);
if(currentHealth <= 0)
{
currentHealth = 0;
Die();
}
else
{
StartCoroutine (DamageEffect ());
}
}
Does anyone of you know how I can solve this problem? You would make me very happy!!!
Sarah :)
Answer by MikeNewall · Jun 04, 2014 at 07:34 PM
Create two empty game objects that are parented to your character, one with the mesh collider attached and one with the box collider. Name them appropriately. You can search for these objects and disable/enable them when you crouch/stand.
GameObject standCollider;
GameObject crouchCollider;
void Start(){
standCollider = transform.FindChild("Stand Collider").gameObject;
crouchCollider = transform.FindChild("Crouch Collider").gameObject;
crouchCollider.SetActive(false);
}
void Update () {
if(Input.GetKeyDown(KeyCode.DownArrow)){
standCollider.SetActive(false);
crouchCollider.SetActive(true);
}
if(Input.GetKeyUp(KeyCode.DownArrow)){
crouchCollider.SetActive(false);
standCollider.SetActive(true);
}
}
Your answer
Follow this Question
Related Questions
Mesh collider cost 1 Answer
Turn an objects mesh renderer on upon collision. 1 Answer
Tile-based approach in Unity 1 Answer
3d collider mesh to 2d collider mesh 0 Answers
How to make 3D colliders on sprites? 0 Answers