- Home /
Distinguish between two colliding objects
I need to distinguish the two objects of a collision. They are instances of the same prefab, so they have the same name, the same scripts components etc. But i need that when they collide the first one moves an x = +1 and the second one moves an x = -1. I wrote this code but it doesn't seem to affect the behaviour of the two instances:
function OnTriggerStay (col : Collider){
if ((col.gameObject.tag == "Enemy"){
col.gameObject.transform.position += Vector3(1,0,0);
gameObject.transform.position -= Vector3(1,0,0);
}
}
I need Unity to distinguish the collider object and the collided object and make the two move a bit in opposite directions. Can you help me?
Answer by ScroodgeM · Jul 14, 2012 at 01:06 PM
if u use trigger, you can't get a collision point and normal to get direction, so you need to calc it based on other info, for example - let's take pivots of objects
transform.position is [me]
col.transform.position is [he]
[he]-[me] is direction from [me] to [he], call it [dir]
[dir].normalized is direction with length = 1, use it for calculations [dirNorm]
move [me] away from [he] transform.position -= [dirNorm] * length;
move [he] away from [me] col.transform.position += [dirNorm] * length;