Example #1
0
    private void Update()
    {
        _direction += SteeringBehaviour.Seek(TargetPosition, _rb.velocity, this) * Time.deltaTime;
        _direction  = SteeringBehaviour.Arriving(this, _direction, TargetPosition, distanceToArriveAtTargetLocation, 0.6f);
        //_movement.MoveTowards(_rb, TargetPosition, Time.deltaTime);

        _movement.AssignVelocity(_rb, Vector3.ClampMagnitude(_direction, _maxSpeed));

        if (rotateForwardToDirection)
        {
            //var rotation = Quaternion.identity;
            _rb.transform.LookAt(_movement.LastDirectionFacing + _rb.position, Vector3.up);
            var newRotation = _rb.transform.rotation;
            if (lockRotationX)
            {
                newRotation.x = 0;
            }
            if (lockRotationY)
            {
                newRotation.y = 0;
            }
            if (lockRotationZ)
            {
                newRotation.z = 0;
            }

            _rb.transform.rotation = newRotation;
        }

        Debug.DrawLine(_rb.transform.position, _movement.LastDirectionFacing + _rb.transform.position, Color.blue);
    }
Example #2
0
    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        _direction += SteeringBehaviour.Seek(_playerStateMachine.TargetLocation, _mover.CurrentVelocity, _mover) * Time.deltaTime;

        if (!enableYDirection)
        {
            _direction.y = 0;
        }

        _mover.MoveDirection(_direction);
    }
Example #3
0
    void Update()
    {
        velocity += SteeringBehaviour.Seek(this, target, 2f);
        if (type == INSIDE_TYPE.CIRCLE)
        {
            velocity += SteeringBehaviour.InsideCircle(this, start_pos, radius, target);
        }
        if (type == INSIDE_TYPE.RECTANGLE)
        {
        }
        velocity += SteeringBehaviour.InsideRectangle(this, start_pos, limitX, limitY, target);

        transform.position += velocity * Time.deltaTime;
    }
Example #4
0
    private void Reposition(GameObject targetObject, IAiStateMachine stateMachine, float deltaTime)
    {
        var   targetPosition = targetObject.transform.position;
        float distance       = Vector3.Distance(targetPosition, stateMachine.MoverComponent.CurrentPosition);

        bool isInAttackRange = distance <= attackDistance;
        bool canAttack       = _attackTimer > attackInterval;
        bool isClose         = distance < minDistanceToMaintain;
        bool isFar           = distance > maxDistanceToMaintain;
        bool isTooFar        = distance > stateMachine.FieldOfViewComponent.Radius;
        bool canSeeTarget    = stateMachine.FieldOfViewComponent.GameObjectsInView.Contains(TargetObject);

        if (isTooFar || !canSeeTarget)
        {
            TargetObject = null;
        }

        //if low on life go to flee state

        if (canAttack && isInAttackRange)
        {
            //go to attack state
            Attack(TargetObject);
        }

        if (isClose)
        {
            _direction += SteeringBehaviour.Flee(targetPosition, _direction, stateMachine.MoverComponent) * deltaTime;
        }
        else if (isFar)
        {
            _direction += SteeringBehaviour.Seek(targetPosition, _direction, stateMachine.MoverComponent) * deltaTime;
        }
        else
        {
            var direction = SteeringBehaviour.Seek(targetPosition, _direction, stateMachine.MoverComponent).normalized;
            stateMachine.MoverComponent.MoveDirection(direction);
            _direction = SteeringBehaviour.Arriving(stateMachine.MoverComponent, _direction, targetPosition, maxDistanceToMaintain, .05f);
        }
        //arrival behaviour
        //attack
        //roam around
    }
    void FollowPath()
    {
        /*
         * predictedLocation = is the predicted location the gameobject will reach with
         * its current velocity.
         * pointA = is the vector from the start of the path, to the predicted location.
         * pointB = is the vector from the start of the path, to the end of the path.
         */
        Vector2 predictedLoc = GetPredictedLocation(predictionDistance);

        Vector2 pointA      = Vector2.zero;
        Vector2 pointB      = Vector2.zero;
        Vector2 normalPoint = Vector2.zero;

        float   bestDistance      = startingBestDistance;
        Vector2 targetNormalPoint = Vector2.zero;

        for (int i = 0; i < path.Length; i++)
        {
            pointA = path[i].start;
            pointB = path[i].end;

            /*
             * normalPoint = the point where the shark should be in the path, from its predicted location.
             */
            normalPoint = GetNormalPoint(predictedLoc, pointA, pointB);

            if (normalPoint.x < pointA.x)
            {
                normalPoint = pointA;
            }
            else if (normalPoint.x > pointB.x)
            {
                normalPoint = pointB;
            }

            float normalDistance = VectorUtility.distance(predictedLoc, normalPoint);

            if (normalDistance < bestDistance)
            {
                bestDistance      = normalDistance;
                targetNormalPoint = normalPoint;
            }
        }

        /*
         * normalPoint = the point where the shark should be in the path, from its predicted location.
         */
        Vector2 direction = pointB - pointA;

        direction  = direction.normalized;
        direction *= directionDistance;



        Vector2 target   = targetNormalPoint + direction;
        float   distance = VectorUtility.distance(targetNormalPoint, predictedLoc);

        pathNormalLR.SetPosition(0, predictedLoc);
        pathNormalLR.SetPosition(1, targetNormalPoint);

        desiredVelocityLR.SetPosition(0, predictedLoc);
        desiredVelocityLR.SetPosition(1, transform.position);

        if (distance > radius)
        {
            pathNormalLR.SetColors(Color.red, Color.red);
            sb.Seek(target);
        }
        else
        {
            pathNormalLR.SetColors(Color.green, Color.green);
        }
    }