Hi all,
I have a player object that has a line of sight. I have made a cone with a mesh collider with a script attached to it. The script looks like this:
function OnCollisionEnter(other : Collision)
{
Debug.Log("We have a collision!");
if(other.transform.tag == "Enemy")
{
if(Physics.Linecast(transform.position, other.transform.position))
{
Debug.Log("Enemy is visible");
}
}
else
{
return;
}
I have added the cone as a child to the player object:
Player
___LosCone
What it is supposed to do, is to detect whether an enemy comes into the cone, however, what it does is that the cone becomes part of the entire player object’s collider, instead of just using it’s own. So now if I shoot at the player, it get’s hit when the bullets touch the Los Cone and it is ignoring the script I attached to the cone completely. It works when I make the cone an object on its own. Am I missing something on parenting?
do a google search for “unity compound colliders” there are loads of threads and topics on this kicking around, also have a look at: Unity - Manual: Layer-based collision detection
might help with setting up what collisions you want to let happen
also… wouldn’t you want the LOS to be a trigger rather than a collision… you don’t want the los to “hit” anything in the physics engine?
I think I have never had such a fast solution to my problem
Making it a trigger worked. Thanks a lot!
One additional problem:
The Linecast is not working for me. I am using:
if(!Physics.Linecast(transform.position, other.transform.position, 2))
{
Debug.Log("Enemy is visible");
other.transform.Find("skeleton").renderer.enabled = true;
}
What I think this should do is:
Fire a line between the player and the object that entered into the LosCone. If nothing intersects the line, the enemy has it’s renderer turned on. the Layer of the LosCone’s collider is 2, so the Lincecast is ignoring that.
However, what happens is that when I place another gameObject between the player and the enemy, the Linecast still returns false. Can you see what I am doing wrong?
Edit: it seem Linecast ignores everything if you don’t pass in a LayerMask. I gave the gameObject a layer and added: var layerMask : int = 1 << 9;
This worked fine, but I have no idea why.