1
\$\begingroup\$

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);
    }
}
}

Raycast stays green

Raycast stays green view2

Would highly appreciate your assistance in fixing this issue which is to get the raycast working right throughout the game. Thanks in advance.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ After doing some more research I found the issue....for some reason it seems that raycasts ignore rigidbodies attached to navmeshagents...Thanks a lot for your time....Turning the collision detection parameter in the rigidbodies of the enemy to continious fixed the issue....more details in this thread answers.unity.com/questions/658434/… \$\endgroup\$
    – AfiJaabb
    Commented Nov 13, 2020 at 12:20

1 Answer 1

0
\$\begingroup\$

I don't know why this works, but this is how I got my problem fixed. I believe the problem occurs when you have both a navmesh agent on the object as well as a rigidbody. Remove the rigidbody and it should work pretty well.

But in my case I changed the Collision Detection on the rigidbody component of my Nav Mesh Agent GameObject from 'Discrete', to 'Continuous' instead.This gave me the best results and the Raycast works just as expected.

\$\endgroup\$
1
  • \$\begingroup\$ It took me 4 hours to find out what was wrong and thanks to you I can continue working now. It's such a strange thing and I don't why it has something to to with the navmeshagent. The funny thing is it worked on startup but after a couple of seconds the raycast never responded again. It's such a weird bug and thanks for your help! :) \$\endgroup\$
    – Niki.cpk
    Commented Dec 1, 2021 at 3:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .