*Temporarily* turn off collision between player and enemy
I'm trying to get into Unity's 2D development, so I downloaded the sample game project. The one with the bazooka and the London Bridge. One thing that really bugged me, the very first thing I wanted to change just to make the game PLAYABLE, is to make it so that the player can walk through enemies during "Mercy Invincibility". That is, the period of time after taking damage during which the player cannot take more damage. The way the game is currently set up, collision between player and enemy is always active and they always collide, but the player will simply not take damage under certain circumstances.
How would I set it up such that collision between player and enemy is disabled during a certain period of time, then automatically re-enabled afterward? Do I want to mess with Physics2DSettings menu? Do I want to call the Physics.IgnoreCollision() function? Do I want to add new variables to the player's global variables (like "public bool isTangible")? Am I overlooking something simpler?
Answer by xxmariofer · Feb 11, 2019 at 08:50 PM
hello, you can access the collider of the object using GetComponent() and set the enabled property to false. you can use a coroutine for that.
You've honestly lost me at "and set the enabled property to false". For one thing, it would never turn back to true even if I knew which Property of a Collider2D I was setting to false. It wouldn't quite fix what I'm trying to accomplish anyway. Effectively, now that I've revised my storyboard, I'm curious if it's possible to do something like this:
PlayerHealth.cs
void Awake
{
if (timer > 0) timer--;
else open.Physics2DSettings -> Player/Enemy = true;
}
void OnCollisionEnter2D (Collision2D col)
{
TakeDamage (col.transform);
open.Physics2DSettings -> Player/Enemy = false;
timer = 2f;
}
using the Physics2DSettings menu. Is that possible, or is that going into protected regions of the engine?
you can do something like this
Physics2D.IgnoreLayerCollision(1, 2, true);
you need to check whats the first and second collider layer index, just create 2 layers, one for theplayer and another for the enemy, and change the 1 and 2 of that line for the desired layer.
for setting it to collide again change the true for the false.
Your answer
Follow this Question
Related Questions
How to check for colliders when teleporting the player in a 2D game? 0 Answers
I am new to unity and i am making a clone of a a game in which i have some problem 1 Answer
Layer Collision 0 Answers
Object reference not set to an instance of an object 0 Answers
How to move and rotate to other direction when hitting a wall 1 Answer