I have a very simple raycast that should be looking at the player and determining if it hit. However the raycast is only registering hits from other object colliders, not the players. I have tried capsule and box colliders, neither work. Here is the code;
void Update() {
currentState.UpdateState(this);
if (isListening) {
rayTarget = new Vector3(player.transform.position.x, 1, player.transform.position.z);
if (Physics.Raycast(transform.position, rayTarget, out hit, 10)) {
Debug.DrawLine(transform.position, rayTarget, Color.red, 10);
Debug.Log(hit.collider.gameObject.tag);
if (hit.collider.gameObject.tag == "Player") {
SwitchState(AlertedState);
}
}
}
}
The mistake is in your call to Physics.Raycast. The second parameter is a direction. You need to pass a direction vector instead of a position, which can be effectively obtained with endPosition - startPosition.
The Raycast method has several overloads, however every version actually takes a starting position and a direction vector, not a target position. If you want to cast between two positions, you can use a LineCast instead. This takes two worldspace positions.
You use Raycast but used DrawLine to visualize it which is wrong. You should have used DrawRay to visualize the ray.
Next time, please check the documentation first. Those are tiny mistakes you could have figured out yourself easily.