Collision detection with Line renderer

Hi, I'm trying to detect the collision between a line renderer and a sensor which is a subpart of a player (FPS). Indeed, the sensor moves with the player, so i must put it inside the Main Camera.

If the "player" is already in the scene without any script, it works, but if it's a real player who was connected via the network with scripts as Character Controller and so on, I can only detect the object "Player", and not the subpart "Sensor".

I've tried some things as layers, but I always have that collision with "Player".

Any idea about what is the best way to deal with that ?

This is my code for now :

if(Physics.Raycast(Camera.main.transform.position,ray.direction,hit)
  Debug.Log(hit.collider.tag) // Always print 'Player'
  if (hit.collider.tag == "FrontSensor") {
    ...
  }
  if (hit.collider.tag == "BackSensor") {
  }
}

1 Answer

1

Try to take another look at layers again. I think correct use of layer should solve your problem.

This page may help you

http://unity3d.com/support/documentation/Components/Layers.html

And pay attention to the following code snippet:

// Bit shift the index of the layer (8) to get a bit mask
var layerMask = 1 << 8;
// This would cast rays only against colliders in layer 8.
// But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
layerMask = ~layerMask;