Exemple #1
0
        // TODO: add pathfinding algorithm so it avoids environmental obstacles
        // Attempt to move this animal towards the given position.
        // If the animal cannot successfully move, return false.
        public bool MoveToPosition(Vector3 targetPosition, float speed)
        {
            _behaviorStatus = AnimalBehaviorStatus.MOVING;
            Vector3 currentPosition = transform.position;
            Vector3 targetDirection = targetPosition - currentPosition;
            float   collisionRadius = Stats.CollisionRadius;

            Collider[] colliders = Physics.OverlapSphere(currentPosition, collisionRadius, Global.AnimalLayerMask);
            if (colliders.Length > 1)
            {
                targetDirection = Vector3.zero;
                for (int i = 0; i < colliders.Length; i++)
                {
                    GameObject temporaryObject = colliders[i].gameObject;
                    if (temporaryObject == gameObject || temporaryObject == _targetObject)
                    {
                        continue;
                    }

                    Animal animal = temporaryObject.GetComponent <Animal>();
                    if (CanEatAnimal(animal))
                    {
                        continue;
                    }

                    Vector3 direction = colliders[i].transform.position - currentPosition;
                    float   distance  = direction.magnitude;
                    targetDirection -= (distance - collisionRadius) / collisionRadius * direction;
                }
            }

            RotateTowardsDirection(targetDirection);

            CurrentSpeed = speed;

            Vector3 desiredPosition = currentPosition + (_currentSpeed * Time.deltaTime * _direction);

            if (!global.GetGround().IsValidPosition(desiredPosition))
            {
                return(false);
            }

            transform.position = desiredPosition;
            return(true);
        }
Exemple #2
0
        public bool EatTargetFood()
        {
            _behaviorStatus = AnimalBehaviorStatus.EATING;
            CurrentSpeed    = 0;
            if (_targetFood == null)
            {
                return(false);
            }

            _eatTimer += Time.deltaTime;
            if (_eatTimer > _targetFood.PortionConsumeRate)
            {
                _eatTimer = 0.0f;

                _food = Mathf.Min(_food + _targetFood.GetPortion(), Stats.MaxFood);
            }

            return(true);
        }
Exemple #3
0
        // TODO: implement smarter running away behavior
        public void RunFromTargetAnimal()
        {
            _behaviorStatus = AnimalBehaviorStatus.MOVING;

            Vector3 targetDirection = transform.position - _targetAnimal.transform.position;

            Vector3 currentPosition = transform.position;

            RotateTowardsDirection(targetDirection);

            CurrentSpeed = Mathf.SmoothStep(_currentSpeed, Stats.MaxSpeed, Stats.MaxSpeed * Time.deltaTime);

            Vector3 desiredPosition = currentPosition + (_currentSpeed * Time.deltaTime * _direction);

            if (global.GetGround().IsValidPosition(desiredPosition))
            {
                transform.position = desiredPosition;
            }
        }