Example #1
0
        public static float CheckTargetInsideFOV(Transform target,
                                                 float minimumDetectionDistance, float maxLookAngle,
                                                 Transform lookingPoint, bool checkPlayerInBuildingStatus = true)
        {
            if (target == null)
            {
                return(-1);
            }

            var distanceToPlayer = Vector3.Distance(target.position, lookingPoint.position);

            if (distanceToPlayer > minimumDetectionDistance)
            {
                return(-1);
            }
            if (checkPlayerInBuildingStatus && GlobalData.playerInBuilding)
            {
                return(-1);
            }

            var modifiedPlayerPosition  = new Vector3(target.position.x, 0, target.position.z);
            var modifiedLookingPosition =
                new Vector3(lookingPoint.position.x, 0, lookingPoint.position.z);

            var lookDirection   = modifiedPlayerPosition - modifiedLookingPosition;
            var angleToPlayer   = Vector3.Angle(lookDirection, lookingPoint.forward);
            var normalizedAngle = ExtensionFunctions.To360Angle(angleToPlayer);

            if (normalizedAngle <= maxLookAngle)
            {
                return(normalizedAngle);
            }
            return(-1);
        }
Example #2
0
        private void SetAndLimitPitch()
        {
            var mouseY = Input.GetAxis(MouseY);

            _pitch += -mouseY * verticalSpeed * Time.deltaTime;

            if (_pitch < 0 || _pitch > 360)
            {
                _pitch = ExtensionFunctions.To360Angle(_pitch);
            }

            if (_pitch > minCameraAngle && _pitch < maxCameraAngle)
            {
                var diffToMinAngle = Mathf.Abs(minCameraAngle - _pitch);
                var diffToMaxAngle = Mathf.Abs(maxCameraAngle - _pitch);

                if (diffToMinAngle < diffToMaxAngle)
                {
                    _pitch = minCameraAngle;
                }
                else
                {
                    _pitch = maxCameraAngle;
                }
            }
        }
Example #3
0
        private void RotateVertically()
        {
            float mouseY = Input.GetAxis(Controls.MouseY);

            _pitch += -mouseY * verticalSpeed * Time.deltaTime;

            if (_pitch < 0 || _pitch > 360)
            {
                _pitch = ExtensionFunctions.To360Angle(_pitch);
            }

            if (_pitch > minCameraAngle && _pitch < maxCameraAngle)
            {
                float diffToMinAngle = Mathf.Abs(minCameraAngle - _pitch);
                float diffToMaxAngle = Mathf.Abs(maxCameraAngle - _pitch);

                if (diffToMinAngle < diffToMaxAngle)
                {
                    _pitch = minCameraAngle;
                }
                else
                {
                    _pitch = maxCameraAngle;
                }
            }

            weapons.localRotation = Quaternion.Euler(_pitch, 0, 0);
        }
Example #4
0
        private void StopPlayerOnExtremeSlope()
        {
            if (Physics.Raycast(raycastPoint.position, Vector3.down, out _hit))
            {
                float moveX = Input.GetAxis(Controls.HorizontalAxis);
                float moveZ = Input.GetAxis(Controls.VerticalAxis);

                Vector3 playerAxis = transform.forward;
                if (moveZ < 0)
                {
                    playerAxis = -transform.forward;
                }
                else if (moveX > 0)
                {
                    playerAxis = transform.right;
                }
                else if (moveX < 0)
                {
                    playerAxis = -transform.right;
                }

                float angle           = Vector3.Angle(_hit.normal, playerAxis);
                float normalizedAngle = ExtensionFunctions.To360Angle(angle);

                if (normalizedAngle - 90 > maxAngleAllowed)
                {
                    _playerRB.velocity = Vector3.zero;
                }
            }
        }
        private Vector3 GetSpawningPoint()
        {
            Vector3 destination = referencePoint.position +
                                  referencePoint.forward * spawnBeforeDistance;

            RaycastHit hit;

            if (Physics.Linecast(referencePoint.position, destination, out hit))
            {
                destination = referencePoint.position + referencePoint.forward * (hit.distance - 1);
            }

            if (Physics.Raycast(destination, Vector3.down, out hit))
            {
                destination = hit.point;

                float angle           = Vector3.Angle(hit.normal, transform.forward);
                float normalizedAngle = ExtensionFunctions.To360Angle(angle);

                if (normalizedAngle - 90 > cannonSlopeAllowed)
                {
                    return(Vector3.zero);
                }
                else
                {
                    return(destination);
                }
            }

            return(Vector3.zero);
        }
Example #6
0
        public static float CheckTargetInsideFOV(Transform target,
                                                 float minimumDetectionDistance, float maxLookAngle,
                                                 Transform lookingPoint)
        {
            if (target == null)
            {
                return(-1);
            }

            float distanceToPlayer = Vector3.Distance(target.position, lookingPoint.position);

            if (distanceToPlayer > minimumDetectionDistance)
            {
                return(-1);
            }

            Vector3 modifiedPlayerPosition  = new Vector3(target.position.x, 0, target.position.z);
            Vector3 modifiedLookingPosition =
                new Vector3(lookingPoint.position.x, 0, lookingPoint.position.z);

            Vector3 lookDirection   = modifiedPlayerPosition - modifiedLookingPosition;
            float   angleToPlayer   = Vector3.Angle(lookDirection, lookingPoint.forward);
            float   normalizedAngle = ExtensionFunctions.To360Angle(angleToPlayer);

            if (normalizedAngle <= maxLookAngle)
            {
                return(normalizedAngle);
            }
            else
            {
                return(-1);
            }
        }
Example #7
0
        private void LaunchSingleArmAttack(Vector2 attackPosition)
        {
            BossBullet bulletInstance = (BossBullet)singleArmBulletPrefab.Instance();

            _bulletHolder.AddChild(bulletInstance);

            float   xVelocity      = Mathf.Cos(Mathf.Deg2Rad(_attackVariable_1));
            float   yVelocity      = Mathf.Sin(Mathf.Deg2Rad(_attackVariable_1));
            Vector2 launchVelocity = new Vector2(xVelocity, yVelocity);

            bulletInstance.SetGlobalPosition(attackPosition);
            bulletInstance.LaunchBullet(launchVelocity);

            _attackVariable_1 += singeArmAttackAngleDiff;
            _attackVariable_1  = ExtensionFunctions.To360Angle(_attackVariable_1);
        }
Example #8
0
        private void SetScreenBasedAnimation()
        {
            float moveZ = Input.GetAxis(PlayerConstantData.VerticalAxis);
            float moveX = Input.GetAxis(PlayerConstantData.HorizontalAxis);

            float yRotation = ExtensionFunctions.To360Angle(transform.rotation.eulerAngles.y);

            float vAxisVMovement = moveZ * Mathf.Cos(Mathf.Deg2Rad * yRotation);
            float vAxisHMovement = moveZ * Mathf.Sin(Mathf.Deg2Rad * yRotation);

            float hAxisVMovement = moveX * Mathf.Sin(Mathf.Deg2Rad * yRotation);
            float hAxisHMovement = moveX * Mathf.Cos(Mathf.Deg2Rad * yRotation);

            float vMovement = vAxisVMovement + hAxisVMovement;
            float hMovement = vAxisHMovement + hAxisHMovement;

            playerAnimator.SetFloat(PlayerConstantData.PlayerVerticalMovement, vMovement);
            playerAnimator.SetFloat(PlayerConstantData.PlayerHorizontalMovement, hMovement);
        }
Example #9
0
        private void LaunchBullet()
        {
            float currentShotAngle = _currentAngle;
            float angleDiff        = 360.0f / eachShotBulletCount;

            for (int i = 0; i < eachShotBulletCount; i++)
            {
                BossBullet bossBulletInstance = (BossBullet)bulletPrefab.Instance();
                _bulletHolder.AddChild(bossBulletInstance);

                bossBulletInstance.SetGlobalPosition(_bossAttackPoint.GetGlobalPosition());

                float   xVelocity    = Mathf.Cos(Mathf.Deg2Rad(currentShotAngle));
                float   yVelocity    = Mathf.Sin(Mathf.Deg2Rad(currentShotAngle));
                Vector2 launchVector = new Vector2(xVelocity, yVelocity);
                bossBulletInstance.LaunchBullet(launchVector.Normalized());

                currentShotAngle += angleDiff;
                currentShotAngle  = ExtensionFunctions.To360Angle(currentShotAngle);
            }
        }
Example #10
0
        private void UpdateRotation()
        {
            if (!_triggerHeldDown)
            {
                _shootBowDirectionDisplayRenderer.enabled       = false;
                _shootSlingShotDirectionDisplayRenderer.enabled = false;
                return;
            }

            if (_playerController.PlayerHasBow)
            {
                _shootBowDirectionDisplayRenderer.enabled = true;
            }
            else
            {
                _shootSlingShotDirectionDisplayRenderer.enabled = true;
            }

            float xMovement = Input.GetAxis(ControlConstants.HorizontalShootAxis);
            float yMovement = Input.GetAxis(ControlConstants.VerticalShootAxis);

            bool isMouseMoving = Input.GetAxis(ControlConstants.MouseX) != 0 ||
                                 Input.GetAxis(ControlConstants.MouseY) != 0;

            Vector3 worldPoint = _mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,
                                                                            Input.mousePosition.y, _mainCamera.nearClipPlane));

            if (xMovement == 0 && yMovement == 0 && !isMouseMoving)
            {
                return;
            }

            float rotationAngle = Mathf.Atan2(yMovement, xMovement) * Mathf.Rad2Deg;

            if (isMouseMoving)
            {
                rotationAngle = Mathf.Atan2(worldPoint.y - _playerTransform.position.y,
                                            worldPoint.x - _playerTransform.position.x) * Mathf.Rad2Deg;
            }

            rotationAngle -= _rotationOffset;
            // This is a hack to lock angles going from
            // -180 to 180
            if (rotationAngle < -90)
            {
                rotationAngle = ExtensionFunctions.To360Angle(rotationAngle);
            }

            if (_playerController.PlayerHasBow)
            {
                rotationAngle = Mathf.Clamp(rotationAngle, _directionLockedAngle - _bowAngleRestriction,
                                            _directionLockedAngle + _bowAngleRestriction);
            }
            else
            {
                rotationAngle = Mathf.Clamp(rotationAngle, _directionLockedAngle - _slingShotAngleRestriction,
                                            _directionLockedAngle + _slingShotAngleRestriction);
            }

            _shootBowDirectionDisplay.rotation       = Quaternion.Euler(0, 0, rotationAngle);
            _shootSlingShotDirectionDisplay.rotation = Quaternion.Euler(0, 0, rotationAngle);
        }
        private void RemoteClientUpdate()
        {
            if (isServer)
            {
                return;
            }

            PositionReceivePackage data = ServerPacketManager.GetNextDataReceived();

            if (data == null)
            {
                return;
            }

            Vector2 normalizedPosition = new Vector2(
                ExtensionFunctions.Map(data.percentX, -1, 1,
                                       _screenWrapper.LeftMostPoint, _screenWrapper.RightMostPoint),
                ExtensionFunctions.Map(data.percentY, 1, -1,
                                       _screenWrapper.TopMostPoint, _screenWrapper.BottomMostPoint)
                );

            if (isLocalPlayer && isPredictionEnabled)
            {
                PositionReceivePackage predictedPackage =
                    _predictedPackages.FirstOrDefault(_ => _.timestamp == data.timestamp);
                if (predictedPackage == null)
                {
                    return;
                }

                Vector2 normalizedPredictedPosition = new Vector2(
                    ExtensionFunctions.Map(predictedPackage.percentX, -1, 1,
                                           _screenWrapper.LeftMostPoint, _screenWrapper.RightMostPoint),
                    ExtensionFunctions.Map(predictedPackage.percentY, 1, -1,
                                           _screenWrapper.TopMostPoint, _screenWrapper.BottomMostPoint)
                    );

                if (Vector2.Distance(normalizedPredictedPosition, normalizedPosition) > positionCorrectionThreshold)
                {
                    _playerRb.velocity = new Vector2(data.velocityX, data.velocityY);
                    transform.position = normalizedPosition;
                    SetMovementAnimation(data.movementAnimation);
                }


                if (Mathf.Abs(ExtensionFunctions.To360Angle(data.rotationZ) -
                              ExtensionFunctions.To360Angle(predictedPackage.rotationZ)) >
                    rotationCorrectionThreshold)
                {
                    _roll = data.roll;
                    transform.rotation = Quaternion.Euler(Vector3.forward * data.rotationZ);
                }

                _predictedPackages.RemoveAll(_ => _.timestamp <= data.timestamp);
            }
            else
            {
                transform.position = normalizedPosition;
                _playerRb.velocity = new Vector2(data.velocityX, data.velocityY);
                SetMovementAnimation(data.movementAnimation);

                _roll = data.roll;
                transform.rotation = Quaternion.Euler(Vector3.forward * data.rotationZ);
            }
        }