public static Vector2 GetVelocity(SteeringAgent2D thisAgent, SteeringAgent2D agentA, SteeringAgent2D agentB, float slowingDistance = 1f)
        {
            if (agentA == null || agentB == null)
            {
                return(Vector2.zero);
            }
            if (slowingDistance < 0)
            {
                throw new InvalidOperationException("The slowing distance can't be negative.");
            }

            Vector2 midPoint = (agentA.GetComponent <Rigidbody2D>().position + agentB.GetComponent <Rigidbody2D>().position) / 2;

            float timeToReachMidPoint = (thisAgent.Rigidbody2D.position - midPoint).magnitude / thisAgent.MaxSpeed;

            //Position in the future
            Vector2 AFuturePos = agentA.GetComponent <Rigidbody2D>().position + agentA.GetComponent <Rigidbody2D>().velocity *timeToReachMidPoint;
            Vector2 BFuturePos = agentB.GetComponent <Rigidbody2D>().position + agentB.GetComponent <Rigidbody2D>().velocity *timeToReachMidPoint;

            midPoint = (AFuturePos + BFuturePos) / 2;

            return(Arrive2D.GetVelocity(thisAgent, midPoint, slowingDistance));
        }
Exemplo n.º 2
0
        public static Vector2 GetVelocity(SteeringAgent2D agent, NavMesh2D navMesh, float waypointSeekDistance = 0.5f)
        {
            if (navMesh == null || !navMesh.CurrentWaypoint.HasValue)
            {
                return(Vector2.zero);
            }

            var currentWaypoint = navMesh.CurrentWaypoint.Value;

            if ((agent.Rigidbody2D.position - currentWaypoint).magnitude < waypointSeekDistance)
            {
                navMesh.SetNextWaypoint();
            }

            if (!navMesh.IsFinished())
            {
                return(Seek2D.GetVelocity(agent, navMesh.CurrentWaypoint.Value));
            }
            else
            {
                return(Arrive2D.GetVelocity(agent, navMesh.CurrentWaypoint.Value, 2f));
            }
        }