I'm new to unity and just trying to build a game.
I've been trying to use Raycast to detect if an enemy is next to the player and I can't seem to get the Physics.Raycast working properly.
Everything works fine in the scene mode and at the beginning of the start mode.But then it stops responding.
I checked threads with similar issues, but it seems I have all those covered(the most common issue is also addressed-the objects I am attempting to detect have rigidbodies and box colliders attached to them)
This is the script I'm working with.
public class hitPlayer : MonoBehaviour
{
public GameObject player;
public Transform m_transform;
Animator playerAnimator;
int layerMask;
RaycastHit m_Hit;
float maxDistance = 5f;
void Start()
{
layerMask = 1 << 8;
layerMask = ~layerMask;
playerAnimator = player.GetComponent<Animator>();
}
public void onTouch()
{
bool isHit = Physics.Raycast(m_transform.position, -m_transform.right, out m_Hit, maxDistance, layerMask);
if (isHit)
{
playerAnimator.SetTrigger("leftHit");
}
else
{
playerAnimator.SetTrigger("rightHit");
}
}
void OnDrawGizmos()
{
bool isHit = Physics.Raycast(m_transform.position, -m_transform.right, out hit, maxDistance,layerMask);
Debug.Log(isHit);
if (isHit)
{
Gizmos.color = Color.red;
Gizmos.DrawRay(m_transform.position, -m_transform.right * hit.distance);
}
else
{
Gizmos.color = Color.green;
Gizmos.DrawRay(m_transform.position, -m_transform.right * maxDistance);
}
}
}
Would highly appreciate your assistance in fixing this issue which is to get the raycast working right throughout the game. Thanks in advance.