- Home /
Question by
Danisuper · Aug 13, 2014 at 03:51 PM ·
c#2dcollider2d-physics
Problem with disabled collision in 2D objects
Hi everybody,in my game there's a power-up that when actived it makes you invincible deactivang obstacles' colliders.The problem is when i collide with the obstacles while my power-up is on my player,the player doesn't die but he gets stuck, such as the obstacle's collider stops the player,but it is deactivated! And if this may help the player has attached a rigidbody2D with the fixed angle actived.
This code is attached to the player and it creates the power-up
int inst=0;
public Vector3 cercPos; //position of the shield
public GameObject scudoCerc; //the shield that protects the player
public bool createdScudo;//if the power-up is on
public float diffTime;
public CircleCollider2D colzLama; //obstacle
public BoxCollider2D colzFreccia;//obstacle
void Update () {
cercPos=new Vector3(transform.position.x,transform.position.y,0);
if(inst<2)
{
Instantiate(scudoCerc,cercPos,Quaternion.identity);
inst+=1;
}
if(createdScudo==true)
{
diffTime-=Time.deltaTime;
colzLama.enabled=false;
colzFreccia.enabled=false;
}
if(diffTime<=0)
{
createdScudo=false;
colzLama.enabled=true;
colzFreccia.enabled=true;
}
}
void OnCollisionEnter2D(Collision2D colz)
{
if(colz.gameObject.tag=="shield" && createdScudo==false)
{
inst+=1;
createdScudo=true;
StartCoroutine(ActiveScudo());
}
if(colz.gameObject.tag=="lama" && createdScudo==true)
{
//rigidbody2D.AddForce(new Vector3(1,0,0)*0.1f,ForceMode2D.Impulse);
}
if(colz.gameObject.tag=="freccia" && createdScudo==true)
{
//rigidbody2D.AddForce(new Vector3(1,0,0)*0.1f,ForceMode2D.Impulse);
}
}
IEnumerator ActiveScudo() //This is the timer of the power-up
{
createdScudo=true;
yield return new WaitForSeconds(diffTime);
createdScudo=false;
}
What is wrong? I am stuck in this code by days. Thanks in advance :D
Comment