- Home /
Collision bug.
I have a collision problem. I have a box as a player, and it moves around. Then i got another box (the enemy) that patroles between certain waypoints i have made. The problem is, if i am moving as i hit the enemy, the player dies as it is supposed to. But if i stand still as the enemy hits the player it goes right through, and does not collide with me.
void OnCollisionEnter(Collision hit)
{
if(hit.transform.tag == "Enemy")
{
Die();
}
if(hit.transform.tag == "Ground" || hit.transform.tag == "Wall")
{
rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
}
}
void Die()
{
Instantiate(explosion, transform.position, transform.rotation);
transform.position = spawnPosition;
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
}
}
sorry for offtopic, but... why do you freeze rotations and y-translate every frame via script????? just make a player prefab and check checkboxes in inspector once. And just use prefab to place player on scene and to instantiate and forget about this constraints...
Well that is another way of doing it. The reason i did it was because i had this bug but it is gone now.
Answer by hav_ngs_ru · Feb 02, 2015 at 09:47 PM
try like this:
foreach(ContactPoint cp in hit.contacts)
if(cp.otherCollider.tag == "Enemy")
Die();
Answer by Dr.frederiksen · Feb 03, 2015 at 12:32 PM
Well i figured it out i just threw a rigidbody in the enemy and it worked flawless.
Your answer
Follow this Question
Related Questions
When using URP, colliders stop working 2 Answers
Collision error 1 Answer
Collision returning an error 0 Answers