Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wandering AI in unity C#

Tags:

c#

unity3d

I'm trying to create a wandering AI

I'm using unity standard assets third person AI

but the problem is the AI is only moving to a certain point and it can not

patrol between these points

here's the code ?

how can i modify it to patrol ?

using System;
using UnityEngine;

namespace UnityStandardAssets.Characters.ThirdPerson
{
    [RequireComponent(typeof (UnityEngine.AI.NavMeshAgent))]
    [RequireComponent(typeof (ThirdPersonCharacter))]
    public class AICharacterControl : MonoBehaviour
    {
        public UnityEngine.AI.NavMeshAgent agent { get; private set; }             // the navmesh agent required for the path finding
        public ThirdPersonCharacter character { get; private set; } // the character we are controlling
        public Transform target;                                    // target to aim for


        private void Start()
        {
            // get the components on the object we need ( should not be null due to require component so no need to check )
            agent = GetComponentInChildren();
            character = GetComponent();

            agent.updateRotation = false;
            agent.updatePosition = true;
        }


        private void Update()
        {
            if (target != null)
                agent.SetDestination(target.position);

            if (agent.remainingDistance > agent.stoppingDistance)
                character.Move(agent.desiredVelocity, false, false);
            else
                character.Move(Vector3.zero, false, false);
        }


        public void SetTarget(Transform target)
        {
            this.target = target;
        }
    }
}
like image 203
Arash Avatar asked Apr 21 '17 05:04

Arash


1 Answers

To make the AI patrol between two points, you need to define the second point and change the behaviour of the AI to change target when it gets to the first point. Currently, it will simply move at zero velocity once it reaches its target (i.e. stop).

Without modifying your code too much, you can extend it to move between two positions by doing something like this.

using System;
using UnityEngine;

namespace UnityStandardAssets.Characters.ThirdPerson
{

    [RequireComponent(typeof (UnityEngine.AI.NavMeshAgent))]
    [RequireComponent(typeof (ThirdPersonCharacter))]
    public class AICharacterControl : MonoBehaviour
    {
        public UnityEngine.AI.NavMeshAgent agent { get; private set; }             // the navmesh agent required for the path finding
        public ThirdPersonCharacter character { get; private set; } // the character we are controlling
        public Transform start;
        public Transform end;

        private Transform target;
        private boolean forward = true;

        private void Start()
        {
            // get the components on the object we need ( should not be null due to require component so no need to check )
            agent = GetComponentInChildren<UnityEngine.AI.NavMeshAgent>();
            character = GetComponent<ThirdPersonCharacter>();

            agent.updateRotation = false;
            agent.updatePosition = true;
        }


        private void Update()
        {
            if (target != null)
                agent.SetDestination(target.position);

            if (agent.remainingDistance > agent.stoppingDistance)
            {
                character.Move(agent.desiredVelocity, false, false);
            }
            else
            {
                SetTarget(forward ? start : end);
                forward = !forward;
            }
        }

        public void SetTarget(Transform target)
        {
            this.target = target;
        }
    }
}

As you can see, I've modified the Update() to tell the AI to change target if it gets too close to the current target. There's also an extra Transform definition (start) at the top that need to be set, and a boolean used to store which direction the AI is going.

This code hasn't been tested in Unity so may need some modifications, but it should give you the right idea.

like image 169
southrop Avatar answered Oct 05 '22 22:10

southrop