protected virtual void SkirtHerd()
    {
        var vel   = default(Vector3);
        var speed = GetSpeed();

        var dist = (HerbivoreSensor.Closest.transform.position - transform.position).magnitude;

        if (dist < HerdApproachDistance)
        {
            vel += Steering.Wander(gameObject, ref WanderParameters);
        }
        else
        {
            vel += Steering.Wander(gameObject, ref WanderParameters);

            var evadeFactor = dist < HerdApproachDistance / 2f ? 2f : .5f;
            vel += Steering.Evade(gameObject, HerbivoreSensor.Closest.gameObject, speed) * evadeFactor;
        }

        transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(vel, transform.up), 1f);

        if (ApplyVelocity)
        {
            Rigidbody.velocity = vel.normalized * speed;
        }
    }
예제 #2
0
 public override Vector2 CalculateSteering(Vehicle agent, List <Vehicle> targets)
 {
     if (targets.Count == 0)
     {
         return(Vector2.zero);
     }
     else
     {
         return(Steering.Evade(agent, targets[0], predictTweaker, fleeRange));
     }
 }
예제 #3
0
    void Update()
    {
        if (target == null)
        {
            return;
        }
        float distance = Vector2.Distance(transform.position, target.transform.position);
        float heading  = Mathf.Sign(target.transform.position.x - transform.position.x);

        if (heading > 0)
        {
            motor.facingRight = true;
        }
        else if (heading < 0)
        {
            motor.facingRight = false;
        }

        float x = 0;

        if (distance > maxDistance)
        {
            if (animator.GetBool("hide"))
            {
                animator.SetBool("hide", false);
            }
            x = Steering.Pursue(rigidbody2D, target, maxSpeed).x;
            // Debug.Log("too far " + x);
        }
        else if (distance < minDistance)
        {
            if (animator.GetBool("hide"))
            {
                animator.SetBool("hide", false);
            }
            x = Steering.Evade(rigidbody2D, target, maxSpeed).x;
            // Debug.Log("too close " + x);
        }
        else
        {
            // Debug.Log("stop and fire");
            if (!animator.GetBool("hide"))
            {
                animator.SetBool("hide", true);
            }
        }

        motor.targetVelocity = transform.right * heading * x * maxSpeed;
    }
예제 #4
0
    private void AvoidPlayerUpdate()
    {
        m_CheckForPlayerCounter += Time.deltaTime;
        if (m_CheckForPlayerCounter > CheckForPlayerRate)
        {
            m_CheckForPlayerCounter = 0f;
            var closestPlayer = OctreeManager.Get(OctreeType.Player).GetClosestObject(transform.position);

            var vectorToStart = (new Vector3(m_StartPos.x, transform.position.y, m_StartPos.z) - transform.position).normalized * FloatSpeed;

            //Make sure there is a player in range
            if (closestPlayer != null &&
                (closestPlayer.position - transform.position).sqrMagnitude < CheckForPlayerRange * CheckForPlayerRange)
            {
                var evadeVector = Steering.Evade(gameObject, closestPlayer.gameObject, FloatSpeed);

                //Go to start XZ pos if that is close to the evade vector
                m_FloatVel = Vector3.Angle(evadeVector, vectorToStart) < 90f
                            ? new Vector3(vectorToStart.x, m_FloatVel.y, vectorToStart.z)
                            : new Vector3(evadeVector.x, m_FloatVel.y, evadeVector.z);

                return;
            }

            //Are we not close to our starting position on the horizontal plane
            if ((transform.position - new Vector3(m_StartPos.x, transform.position.y, m_StartPos.z)).sqrMagnitude > 2f)
            {
                //Float towards start on the horizontal
                m_FloatVel = new Vector3(vectorToStart.x, m_FloatVel.y, vectorToStart.z);
            }
            else
            {
                m_FloatVel = new Vector3(0, m_FloatVel.y, 0);
            }
        }
    }