I was trying to use OnCollisionEnter, I know it detect collision on every object with collider, I want to specify which object it will detect collision with. How I do it in java script?
system
2
You can use something like
function OnCollisionEnter(Bam : Collision)
{
if(Bam.collider.gameObject.name == "whatIWannaHit")
{
//What you wanna do on collision with GameObject "whatIWannaHit"
}
}
However, if you want achieve something like 'only hits enemies', it might be more convenient to use tags. You can for example create a tag named "enemy" and tag all enemy GameObjects with that. Then you can check for the tag like that:
function OnCollisionEnter(Bam : Collision)
{
if(Bam.tag == "enemy")
{
//What you wanna do on collision with GameObject tagged "enemy"
}
}