- Home /
Linecast detecting collision from target
Pretty simple question, I have no idea what a solution would be though. Im just trying to make a simple linecast, that goes from an AI to my player. At the moment, Im just trying to make it so if it detects a collider on the way to the target, it feeds back "Collider", and if not, "No Collider". Pretty simple. But the issue is, it seems its including the collider around my player as a collider thats blocking the line. If I turn the collider off, which I obviously wouldnt want to be in game, then it works.
Heres the code
#pragma strict
var PlayerTarget : Transform;
var TestState : String;
function Start () {
}
function Update () {
Debug.DrawLine (transform.position, PlayerTarget.position, Color.red);
if (Physics.Linecast (transform.position, PlayerTarget.position)) {
TestState = "Collider";
}
else{
TestState = "No Collider";
}
}
(I use a string rather than Debug.Log just because its easier for me to read when theres a lot of stuff in the debug log)
Answer by troien · Nov 25, 2014 at 06:15 PM
According to the docs of Physics.Linecast, you can give a third argument called LayerMask which: 'is used to selectively ignore colliders when casting a ray.'
By using that argument, and putting your AI/Player in the correct layer you can fix your problem.
Thanks mate, although its confusing me a little. So I made a custom layer, layer10 (called Player), I added ,10)) to the end of my physics.linecast line. But whether its the AI and the obstacle, or the player, actually on that layer, it doesnt work.
That is because it is a bit mask. This means that ins$$anonymous$$d of using the int value, it checks each bit. An int has 32 bits so there can be a maximum of 32 layers. (The int is basically used like an array of 32 booleans).
This also means that the value 10 actually means 00000000000000000000000000001010, which means layers '1 and 3'.
To get the correct value through code you will have to bit shift.
For layer 10 this would be '1 << 10' (this will set your mask to 00000000000000000000010000000000) (int value 1024)
Layer 10 and 8 would be '(1 << 10 | 1 << 8)' (this will set your mask to 00000000000000000000010100000000) (int value 1280)
Another (easier) way would be to have a public Layer$$anonymous$$ask variable, set it in the inspector and simply use layer$$anonymous$$ask.value
Also note that adding a ~ before the value inverts it so
' (1 <<10)' results in: 00000000000000000000010000000000
'~(1 << 10)' results in 11111111111111111111101111111111
Answer by zak-reynolds · Nov 25, 2014 at 06:29 PM
The most simple solution would be to set your player's GameObject's layer to "IgnoreRaycast" if possible (it would work if you're not putting it on a particular layer for some reason, and you don't need to raycast against the player for any other reason).
Another would be to set up a custom LayerMask for your Linecast, and have it ignore the player's layer. This page (http://docs.unity3d.com/Manual/Layers.html) has some info that can help, but here's a snippet:
// Bit shift the index of the layer to get a bit mask
// Assumes that the player is on a layer called "Player"
int layerMask = 1 << LayerMask.NameToLayer("Player");
// This would cast rays only against colliders in the "Player" layer.
// But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
layerMask = ~layerMask;
RaycastHit hit;
if (Physics.Linecast (transform.position, PlayerTarget.position, out hit, layerMask))
{
// etc...
// You can use "hit.collider" to get the other collider and GameObject
Damn, that first solution would have been so easy, but my player does rely on both casting and receiving raycasts other than this one
Your answer
Follow this Question
Related Questions
Physics.Raycast not hitting anything 1 Answer
Linecast from raycast hit point 1 Answer
layerMask in Linecast doesn't work? 1 Answer
Linecast inside a box collider 0 Answers