RayCast Problem

I’m having more issues with Raycast. Such a royal pain to use these things lol.

This time I’m trying to cast a ray from an enemy unit to my Player. I’m using this code to do so:

currentLocPlayer = objPlayer.transform.position;
ray = new Ray(transform.position, currentLocPlayer);
                    Physics.Raycast(ray, out hit);
                    if (hit.collider.tag == "Player"  hit.collider != this)
                    {
                        inputMovement = objPlayer.transform.position - transform.position;
                        isMoving = true;
                    }

When I run the game and get within range for it to do this part of the script, I get an error “Object reference not set to an instance of an object.” Occasionally when I get this error, it will cause my Player to move slowly across the screen.

am I doing something wrong with my ray? I just want it to be casted towards the player to do a check to see if any obstacles are in the way. Even if there aren’t any, it still doesn’t work.

first things first. You need to run your if test against the raycast… see code below, if it never hits anything, hit, will be empty, thus giving you an error if you try to use it…

	hit RaycastHit;
	currentLocPlayer = objPlayer.transform.position;
	ray = new Ray(transform.position, currentLocPlayer);
	if(Physics.Raycast(ray, out hit)){
		if (hit.collider.tag == "Player"  hit.collider != this)
		{
			inputMovement = objPlayer.transform.position - transform.position;
			isMoving = true;
		}
	}

A couple of things I may suggest. Put all of your players and enemies on Layer 2 (ignore raycast) and run tests to see if nothing is between them… :wink:

Also, raise your target hit point if your characters origin points are at their feet. This will prevent things on the ground from blocking your view.

	testPoint = objPlayer.transform.position + Vector3(0,2,0);
	myPoint =  transform.position + Vector3(0,2,0);
	hit RaycastHit;
	if(!Physics.Linecast(myPoint, testPoint, hit)){
		inputMovement = objPlayer.transform.position - transform.position;
		isMoving = true;
	}

I assume you are writing this in C#. so I made the variables match that format

You need to test to make sure hit actually hit something!
if ( Physics.Raycast(ray, out hit) hit.collider.tag == “Player” hit.collider != this)

This also, would cause an error since hit, is not populated until the if statement is confirmed. :wink:

If I were pointing the ray in the direction of the player, wouldn’t it be guaranteed to always hit something? I assume the ray is cast instantaneously, so you can’t outrun it.

I will run some tests to see if I’m even hitting anything. This is all in 2D (using x and z axis), so I don’t need to offset the ray and I’ve also made sure the ground plane is .1 lower than the player/enemy to avoid the ray hitting the ground.

I can almost guarantee when I run these tests it will return nothing hit. With the currentPlayerLoc variable, do I need to normalize it? Because at the moment it is picking out the exact location of the player instead of a direction between 0,0,0 and 1,1,1.

EDIT: I threw in the thing pakfront told me to do (easier to do right off the bat). It started working ONLY if the Player was in front of the enemy. I threw in a debug.drawline to show the ray and it did indeed show it. However, I don’t want the ray to only be cast in front of the enemy. I want it to be cast in any direction, no matter the enemy’s rotation.

That would be the Linecast instead of the Raycast. Linecasts are sooo much easier.

Also, I am not sure that in your tests you will not have to reference “this.collider” instead of “this”

currentLocPlayer = objPlayer.transform.position;
if(Physics.Linecast(transform.position, currentLocPlayer  , out hit)){
if (hit.collider.tag == "Player"  hit.collider != this.collider)
{
inputMovement = objPlayer.transform.position - transform.position;
isMoving = true;
}
}

oh man I didn’t know Linecast existed. Worked like a charm! Thank you!

I also changed this to this.collider. Dunno if it made a difference, but it works =).