Advertisement

Unity Agent: My agent accidentally open doors on his way, when he is not supposed to.

Started by December 31, 2020 02:58 PM
0 comments, last by babaliaris 3 years, 10 months ago

Take a look at the following video. The moving capsule is trying to reach the red cube but on its way, it accidentally opens a door that it's not supposed to open.

I know why this is happening. My logic says “If the distance between any door and the agent < 4, then open the door”. The problem is that I can not figure out how to make him smart enough, to be able to realize that he is not heading for this door, he just happens to pass right next to it, so don't open it!

This is the door logic:

//Enemy Distance Test.
    private void EnemyDistanceCheck()
    {
        //For each enemy.
        foreach (Transform enemy in Enemies) 
        {

            //Calculate the distance between the door and the enemy.
            float distance = Vector3.Distance(transform.position, enemy.position);

            //Get the dot product to figure out if the enemy is in front or in the back of the door.
            float dotBetweenTheDoorAndThePlayer = Vector3.Dot(transform.forward.normalized, enemy.forward.normalized);

            //Get the nav mesh component of the enemy.
            NavMeshAgent agent = enemy.GetComponent<NavMeshAgent>();

            //Get the enemy general script.
            Enemy enemyScript = enemy.GetComponent<Enemy>();

            //Get the enemy navigation script.
            EnemyNav enemyNavScript = enemy.GetComponent<EnemyNav>();

            //The enemy is in the correct distance to be able to open the door.
            if (distance < 4)
            {

                //The player is in front of the door.
                if (dotBetweenTheDoorAndThePlayer > 0) 
                {

                    //Get the door's Rigidbody.
                    Rigidbody doorRigidbody = m_door.GetComponent<Rigidbody>();

                    //Make it non kinematic.
                    doorRigidbody.isKinematic = false;

                    //Add a force to open the door.
                    doorRigidbody.AddForce(m_door.forward * enemyScript.EnemyDoorPushForce);
                }


                //The player is in the back face of the door.
                else 
                {
                }
            }
        }
    }

void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

This topic is closed to new replies.

Advertisement