Пример #1
0
        protected void UpdateLookInput()
        {
            bool    foundTargetEntity = false;
            bool    isMobile          = InputManager.useMobileInputOnNonMobile || Application.isMobilePlatform;
            Vector2 lookDirection;

            if (isMobile)
            {
                // Turn character by joystick
                lookDirection = new Vector2(InputManager.GetAxis("Mouse X", false), InputManager.GetAxis("Mouse Y", false));
                Transform   tempTransform;
                IGameEntity tempGameEntity;
                Vector3     tempTargetPosition;
                int         pickedCount;
                if (GameInstance.Singleton.DimensionType == DimensionType.Dimension2D)
                {
                    pickedCount = physicFunctions.Raycast(PlayerCharacterEntity.MeleeDamageTransform.position, lookDirection, 100f, Physics.DefaultRaycastLayers);
                }
                else
                {
                    pickedCount = physicFunctions.Raycast(PlayerCharacterEntity.MeleeDamageTransform.position, new Vector3(lookDirection.x, 0, lookDirection.y), 100f, Physics.DefaultRaycastLayers);
                }
                for (int i = pickedCount - 1; i >= 0; --i)
                {
                    aimTargetPosition = physicFunctions.GetRaycastPoint(i);
                    tempTransform     = physicFunctions.GetRaycastTransform(i);
                    tempGameEntity    = tempTransform.GetComponent <IGameEntity>();
                    if (tempGameEntity != null)
                    {
                        foundTargetEntity = true;
                        CacheUISceneGameplay.SetTargetEntity(tempGameEntity.Entity);
                        SelectedEntity = tempGameEntity.Entity;
                        if (tempGameEntity.Entity != PlayerCharacterEntity.Entity)
                        {
                            // Turn to pointing entity, so find pointing target position and set look direction
                            if (!doNotTurnToPointingEntity)
                            {
                                // Find target position
                                if (tempGameEntity is IDamageableEntity)
                                {
                                    tempTargetPosition = (tempGameEntity as IDamageableEntity).OpponentAimTransform.position;
                                }
                                else
                                {
                                    tempTargetPosition = tempGameEntity.GetTransform().position;
                                }
                                // Set look direction
                                if (GameInstance.Singleton.DimensionType == DimensionType.Dimension2D)
                                {
                                    lookDirection = (tempTargetPosition - CacheTransform.position).normalized;
                                }
                                else
                                {
                                    lookDirection = (XZ(tempTargetPosition) - XZ(CacheTransform.position)).normalized;
                                }
                            }
                        }
                        break;
                    }
                }
            }
            else
            {
                // Turn character follow cursor
                lookDirection = (InputManager.MousePosition() - new Vector3(Screen.width, Screen.height) * 0.5f).normalized;
                // Pick on object by mouse position
                Transform   tempTransform;
                IGameEntity tempGameEntity;
                Vector3     tempTargetPosition;
                int         pickedCount = physicFunctions.RaycastPickObjects(CacheGameplayCamera, InputManager.MousePosition(), Physics.DefaultRaycastLayers, 100f, out _);
                for (int i = pickedCount - 1; i >= 0; --i)
                {
                    aimTargetPosition = physicFunctions.GetRaycastPoint(i);
                    tempTransform     = physicFunctions.GetRaycastTransform(i);
                    tempGameEntity    = tempTransform.GetComponent <IGameEntity>();
                    if (tempGameEntity != null)
                    {
                        foundTargetEntity = true;
                        CacheUISceneGameplay.SetTargetEntity(tempGameEntity.Entity);
                        SelectedEntity = tempGameEntity.Entity;
                        if (tempGameEntity.Entity != PlayerCharacterEntity.Entity)
                        {
                            // Turn to pointing entity, so find pointing target position and set look direction
                            if (!doNotTurnToPointingEntity)
                            {
                                // Find target position
                                if (tempGameEntity is IDamageableEntity)
                                {
                                    tempTargetPosition = (tempGameEntity as IDamageableEntity).OpponentAimTransform.position;
                                }
                                else
                                {
                                    tempTargetPosition = tempGameEntity.GetTransform().position;
                                }
                                // Set look direction
                                if (GameInstance.Singleton.DimensionType == DimensionType.Dimension2D)
                                {
                                    lookDirection = (tempTargetPosition - CacheTransform.position).normalized;
                                }
                                else
                                {
                                    lookDirection = (XZ(tempTargetPosition) - XZ(CacheTransform.position)).normalized;
                                }
                            }
                        }
                        break;
                    }
                }
            }
            if (!foundTargetEntity)
            {
                CacheUISceneGameplay.SetTargetEntity(null);
                SelectedEntity = null;
            }

            // Set aim position
            if (setAimPositionToRaycastHitPoint)
            {
                PlayerCharacterEntity.AimPosition = PlayerCharacterEntity.GetAttackAimPosition(ref isLeftHandAttacking, aimTargetPosition);
                if (GameInstance.Singleton.DimensionType == DimensionType.Dimension3D)
                {
                    Quaternion aimRotation = Quaternion.LookRotation(PlayerCharacterEntity.AimPosition.direction);
                    PlayerCharacterEntity.Pitch = aimRotation.eulerAngles.x;
                }
            }
            else
            {
                PlayerCharacterEntity.AimPosition = PlayerCharacterEntity.GetAttackAimPosition(ref isLeftHandAttacking);
            }

            // Turn character
            if (lookDirection.sqrMagnitude > 0f)
            {
                if (GameInstance.Singleton.DimensionType == DimensionType.Dimension2D)
                {
                    PlayerCharacterEntity.SetLookRotation(Quaternion.LookRotation(lookDirection));
                }
                else
                {
                    PlayerCharacterEntity.SetLookRotation(Quaternion.LookRotation(new Vector3(lookDirection.x, 0, lookDirection.y)));
                }
            }
        }