- Home /
The question is answered, right answer was accepted
Need Help Using LinecastNonAlloc
I am trying to make a game in which a zombie chases a player. I am trying to use Linecast to see if the zombie can actually see the player, and seeing as I am using Linecast a lot (every tick, is this even good?) I decided to use LinecastNonAlloc. When I use this it comes up with the error: "Argument #3 cannot convert RaycastHit2D to type RaycastHit2D[]" but when I set the variable to RaycastHit2D[] then it doesn't do the hit.collider and hit.transform so I can find what object it hit. Could there be an easier way to do this? These are the variables and the code that I am using.
private RaycastHit2D hit;
private int number;
void LineOfSight () {
number = Physics2D.LinecastNonAlloc(transform.position, player.transform.position, hit);
Debug.Log (hit);
Debug.Log (number);
if (hit.collider != null) {
Transform objectHit = hit.transform;
Debug.Log (objectHit.name);
}
Any help would be appreciated, thanks.
Answer by Bunny83 · Oct 11, 2017 at 12:36 PM
As killer_mech said the method expects an array and not a single RaycastHit2D.
However you have to initialize the array with a certain length. Arrays can't change their length. If you initialize it with 30 elements, the raycast can return "up to" 30 elements. How many elements were actually set in the array is returned as number. So if the array length is 0 it can only return 0 elements. That's why you always get back "0".
private RaycastHit2D[] hit = new RaycastHit2D[ 30 ];
You just have to allocate the array "large enough" to be able to hold all wanted hits. If the returned number is equal to your allocated array size you either (by chance) hit exactly that amount of collider or the method ran out of space and just returned as many as it could fit into the array.
To finally iterate through the array you can not use "foreach". You have to use a normal for loop as you can only iterate up to the number that the raycast method returned.
for(int i = 0; i < number; i++)
{
// do something with hit[ i ]
}
The important thing is that you allocate the array once and then just reuse it every time. You don't have to check if the collider of a hit is null or not as the raycast method will only include actual hits.
Answer by killer_mech · Oct 11, 2017 at 04:26 AM
@BorisOZ You cannot use RaycastHit2D when it is already mentioned the results are given in an array RaycastHit2D[]. The number is all the results of Raycast in the array. You just have to iterate through the RaycastHit2D[] array to get your result. You cannot directly write RaycastHit2D[] variable to hit.collider and hit.transform. So the correct code will be
private RaycastHit2D[] hit;
private int number;
void LineOfSight()
{
number = Physics2D.LinecastNonAlloc(transform.position, player.transform.position, hit);
Debug.Log(number);
if(number > 0)
{
foreach(RaycastHit2D rc in hit)
{
if (rc.collider != null)
{
Transform objectHit = rc.transform;
Debug.Log(objectHit.name);
}
}
}
}
As you see you need to just iterate the loop. Please read unity wiki (LinecastNonAlloc) for more details always.
That means your line cast is not hitting anything. Check if anything you are missing . Try to do linecast first with a static object with collider and see if that returns any result. I did not test the code. I wrote only the corrections to your code.
Do I need to declare rc as a variable at the start? It's saying "NullReferenceException: Object reference not set to an instance of an object"
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to calculate angular velocity after a collision? 0 Answers
AI movement in D 0 Answers
Help with 2D physics script 1 Answer