Hey there.
So I’m working on a simple Enemy-AI which shoots Raycasts to determine where the Player is.
So, here’s what I’m doing:
- the enemy patrols in his route (startPosition and endPosition)
- the “Side”-Raycast is firing and checking if it’s hitting the Player or something else
- if it hits the Player it’s going to chase the Player, if near enough, it will attack him
- if it doesn’t hit the Player but other objects tagged as “EnemyWall” then it will change it’s movement-direction and go the other way around
Now I use multiple Raycasts, which works, too, but not quite the way I want it to.
Here’s the code excerpt:
//excerpt from my "DoDecide"-Method
Debug.DrawRay(transform.position, new Vector3(rayDirection.x*direction, rayDirection.y, rayDirection.z)*rayLength, Color.green);
Debug.DrawRay(transform.position, new Vector3(rayDirection2.x*direction, rayDirection2.y, rayDirection2.z)*rayLength2, Color.green);
////// FIRST RAYCAST
if(Physics.Raycast (transform.position, new Vector3(rayDirection.x*direction, rayDirection.y, rayDirection.z), out hit, rayLength))
{
if (hit.collider.gameObject.tag == "Player")
{
Debug.Log("HITPLAYER");
if(_distanceToPlayer <= 2.0f)
{
return ENEMYSTATE.ATTACK;
}
else
return ENEMYSTATE.CHASE;
}
else if (hit.collider.gameObject.tag == "EnemyWall")
{
Debug.Log("HITWALL");
direction *= -1;
}
}
////// SECOND RAYCAST
if(Physics.Raycast (transform.position, new Vector3(rayDirection2.x*direction, rayDirection2.y, rayDirection2.z), out hit, rayLength2))
{
if (hit.collider.gameObject.tag == "Player")
{
Debug.Log("HITPLAYER RAY2");
if(_distanceToPlayer <= 2.0f)
{
return ENEMYSTATE.ATTACK;
}
else
return ENEMYSTATE.CHASE;
}
else if (hit.collider.gameObject.tag == "EnemyWall")
{
Debug.Log("HITWALL RAY2");
direction *= -1;
}
}
Here’s the Ray-Setup in the Inspector:
RayLength 1 and 2 are simple floats for the actual length of the fired Ray.
And RayDirection and RayDirection2 are the Direction-Vectors which are used in the code.
And here’s the problem:
As you see in the next picture, I’m sending two Rays. One Ray, the diagonal one, has a longer length, just for test-purposes.
So that’s what happening:
- the big collider on the left is tagged as “EnemyWall”
→ the idea is, as soon as the diagonal Ray hits this Wall the Enemy (the little cube) should move in the other direction
BUT that’s not happening till the Ray is a little bit inside of the collider and that’s bad. That should happen immediately.
Beyond that the Debug.Log gives different results. The diagonal Ray is the second Ray, but when it hits the Wall the Debug.Log from the first Ray is logged, which is weird.
So, that’s the problem and of course there is a solution.
As soon as I use these two lines of code in Update(), the Raycasts behave how they should:
rayDirection.Normalize();
rayDirection2.Normalize();
As soon the Rays hit the Wall, the correct Logs are printed and the character moves immediately away from them. No Rays entering the Collider.
I’m not a math-genius, but I do understand the idea behind normalizing a Vector. But why does this help exactly? Running the game with this addition of code I look at the Vector values in the Inspector and get of course the normalized values of x and y:
x: 0.7071068
y: 0.7071068
But where is the differene? Is it legit to use .Normalize() that way in Update() or should I change something?