private GoldPlayerController SetupPlayer()
        {
            GameObject           playerGO         = new GameObject("[TEST] Test Player");
            GoldPlayerController playerController = playerGO.AddComponent <GoldPlayerController>();
            GameObject           playerCameraHead = new GameObject("[TEST] Test Player Camera Head");
            Camera camera = playerCameraHead.AddComponent <Camera>();

#if ENABLE_INPUT_SYSTEM && UNITY_2019_3_OR_NEWER
            GoldPlayerInputSystem input = playerGO.AddComponent <GoldPlayerInputSystem>();
#else
            GoldPlayerInput input = playerGO.AddComponent <GoldPlayerInput>();
#endif
            playerController.GetComponent <CharacterController>().center = new Vector3(0, 1, 0);
            playerController.Camera.CameraHead = playerCameraHead.transform;
            playerController.Camera.FieldOfViewKick.EnableFOVKick = false;
            playerController.HeadBob.BobTarget = playerCameraHead.transform;

            playerController.Movement.MovingPlatforms.Enabled = true;
            playerController.Movement.MovingPlatforms.Initialize(playerController, input);

            //GameObject raycastWeaponGO = new GameObject("[Test] Raycast Weapon");
            //GoldPlayerWeapon raycastWeapon = raycastWeaponGO.AddComponent<GoldPlayerWeapon>();
            //raycastWeapon.ProjectileType = GoldPlayerWeapon.ProjectileTypeEnum.Raycast;
            //raycastWeapon.ShootOrigin = playerCameraHead.transform;

            //GoldPlayerWeapons weapons = playerGO.AddComponent<GoldPlayerWeapons>();
            //weapons.AvailableWeapons = new GoldPlayerWeapon[] { raycastWeapon };
            //weapons.AddWeapon(raycastWeapon);

            return(playerController);
        }
        /// <summary>
        /// Initializes the audio behaviour.
        /// </summary>
        /// <param name="footstepsSource"></param>
        /// <param name="jumpSource"></param>
        /// <param name="landSource"></param>
        public void Initialize(GoldPlayerController playerController, GoldInput input, AudioSource footstepsSource, AudioSource jumpSource, AudioSource landSource)
        {
            PlayerController = playerController;
            PlayerInput      = input;

            FootstepsSource = footstepsSource;
            JumpSource      = jumpSource;
            LandSource      = landSource;
        }
        public IEnumerator GoldPlayerInitializeGeneratesGarbage()
        {
            GoldPlayerController player = SetupPlayer();

            player.InitOnStart = false;
            Assert.That(() =>
            {
                player.Initialize();
            }, Is.Not.AllocatingGCMemory());
            yield return(null);
        }
        protected override GoldPlayerController SetupPlayer()
        {
            GoldPlayerController player = base.SetupPlayer();

            interaction                  = player.gameObject.AddComponent <GoldPlayerInteraction>();
            interaction.CameraHead       = player.Camera.CameraHead;
            interaction.InteractInput    = "Interact";
            interaction.InteractionLayer = 1;

            return(player);
        }
        public IEnumerator MovingPlatformGeneratesGarbage()
        {
            // Use the Assert class to test conditions
            GoldPlayerController player = SetupPlayer();

            yield return(null);

            Assert.That(() =>
            {
                player.Movement.MovingPlatforms.OnUpdate(Time.deltaTime);
            }, Is.Not.AllocatingGCMemory());
        }
        public IEnumerator InitializeMovingPlatformsGeneratesNoGarbage()
        {
            GoldPlayerController player          = SetupPlayer();
            MovingPlatformsClass movingPlatforms = new MovingPlatformsClass();

            Assert.That(() =>
            {
                movingPlatforms.Initialize(player, null);
            }, Is.Not.AllocatingGCMemory());

            yield return(null);
        }
Beispiel #7
0
        /// <summary>
        /// Sets the player and finds all required components.
        /// </summary>
        private void SetPlayer(GoldPlayerController player)
        {
#if GOLD_PLAYER_INTERACTION
            // Only get the interaction if the previous set player isn't the new player.
            if (player != null && this.player != player)
            {
                playerInteraction = player.GetComponent <GoldPlayerInteraction>();
            }
#endif
            // Set the player.
            this.player = player;
        }
        public IEnumerator SetupScene()
        {
            Time.timeScale = 1;

            player = SetupPlayer();
            CreateTestScene();

#if UNITY_EDITOR
            yield return(new EnterPlayMode());
#else
            yield return(null);
#endif
        }
        public IEnumerator GoldPlayerUpdateGeneratesGarbage()
        {
            GoldPlayerController player = SetupPlayer();

            yield return(null);

            Assert.That(() =>
            {
#if HERTZLIB_UPDATE_MANAGER
                player.OnUpdate();
#else
                player.Update();
#endif
            }, Is.Not.AllocatingGCMemory());
        }
        protected virtual GoldPlayerController SetupPlayer()
        {
            GameObject playerGO = new GameObject("[TEST] Test Player", typeof(CharacterController));

            playerGO.transform.position = new Vector3(0, 0.08f, 0);
            playerGO.layer = 31;

            GameObject bobTarget = new GameObject("[TEST] Test Player Bob Target");

            bobTarget.transform.SetParent(playerGO.transform);
            bobTarget.transform.localPosition = new Vector3(0, 1.6f, 0);

            GameObject playerCameraHead = new GameObject("[TEST] Test Player Camera Head");

            playerCameraHead.transform.SetParent(bobTarget.transform);
            playerCameraHead.transform.localPosition = new Vector3(0, 0, 0);
            Camera camera = playerCameraHead.AddComponent <Camera>();

            input = playerGO.AddComponent <GoldPlayerTestInput>();

            GoldPlayerController playerController = playerGO.AddComponent <GoldPlayerController>();

            playerController.GetComponent <CharacterController>().center = new Vector3(0, 1, 0);

            playerController.Camera.CameraHead   = playerCameraHead.transform;
            playerController.Camera.TargetCamera = camera;
            playerController.Camera.FieldOfViewKick.EnableFOVKick = true;

            playerController.HeadBob.BobTarget = bobTarget.transform;

            playerController.Movement.GroundLayer = 1;

            playerController.Movement.MoveInput   = GoldPlayerTestInput.MOVE;
            playerController.Movement.JumpInput   = GoldPlayerTestInput.JUMP;
            playerController.Movement.RunInput    = GoldPlayerTestInput.RUN;
            playerController.Movement.CrouchInput = GoldPlayerTestInput.CROUCH;

            playerController.Camera.LookInput = GoldPlayerTestInput.LOOK;

            playerController.InitOnStart = false;
            playerController.GetReferences();
            playerController.Initialize();

            return(playerController);
        }
        public IEnumerator PlayerFollowsMovingPlatform()
        {
            // Use the Assert class to test conditions
            GoldPlayerController player   = SetupPlayer();
            GameObject           platform = new GameObject("Platform");

            platform.AddComponent <BoxCollider>();
            platform.tag = "Respawn";
            player.transform.position = platform.transform.position + new Vector3(0, 0.5f, 0);
            yield return(null);

            yield return(null);

            platform.transform.position = new Vector3(10, 0, 10);
            yield return(null);

            Assert.AreEqual(new Vector3(player.transform.position.x, 0, player.transform.position.z), new Vector3(platform.transform.position.x, 0, platform.transform.position.z));
        }
    private void Start()
    {
        gunManager     = FindObjectOfType <GunManager>();
        player         = GetComponent <GoldPlayerController>();
        initialGravity = player.Movement.Gravity;

        // Check if there's water
        WaterBase water = FindObjectOfType <WaterBase>();

        if (water == null)
        {
            // Script is redundant, bye bye
            Destroy(this);
        }
        else
        {
            waterPlane = water.transform;
        }
    }
Beispiel #13
0
        protected override GoldPlayerController SetupPlayer()
        {
            GoldPlayerController player = base.SetupPlayer();

            graphics = player.gameObject.AddComponent <GoldPlayerGraphics>();

            GameObject graphicsParent = new GameObject("[TEST] Player Graphics");

            graphicsParent.transform.SetParent(player.transform);

            GameObject graphicsCube = GameObject.CreatePrimitive(PrimitiveType.Cube);

            graphicsCube.transform.SetParent(graphicsParent.transform);
            graphicsCube.GetComponent <Renderer>().shadowCastingMode = ShadowCastingMode.TwoSided;

            GameObject emptyParent = new GameObject();
            GameObject newCube     = GameObject.CreatePrimitive(PrimitiveType.Cube);

            emptyParent.transform.SetParent(graphicsParent.transform);
            newCube.transform.SetParent(emptyParent.transform);

            graphics.Objects = new GoldPlayerGraphics.GraphicsObject[]
            {
                new GoldPlayerGraphics.GraphicsObject()
                {
                    Target            = graphicsCube.transform,
                    IsParent          = false,
                    WhenMyGraphics    = HandleGraphics.DisableTarget,
                    WhenOtherGraphics = HandleGraphics.EnableTarget
                },
                new GoldPlayerGraphics.GraphicsObject()
                {
                    IsParent          = true,
                    Target            = emptyParent.transform,
                    WhenMyGraphics    = HandleGraphics.DisableRenderers,
                    WhenOtherGraphics = HandleGraphics.EnableRenderers
                }
            };

            graphics.Owner = GraphicsOwner.Me;

            return(player);
        }
Beispiel #14
0
        private void OnEnable()
        {
            currentTab = EditorPrefs.GetInt(SELECTED_TAB_PREFS, 0);

            if (currentTab < 0)
            {
                currentTab = 0;
            }

            if (currentTab > 4)
            {
                currentTab = 4;
            }

            camera   = serializedObject.FindProperty("camera");
            movement = serializedObject.FindProperty("movement");
            headBob  = serializedObject.FindProperty("headBob");
            audio    = serializedObject.FindProperty("audio");

            goldPlayer          = (GoldPlayerController)target;
            characterController = goldPlayer.GetComponent <CharacterController>();
        }
Beispiel #15
0
        public IEnumerator TestInitOnStart()
        {
            GameObject tempPlayer = new GameObject();

            tempPlayer.AddComponent <GoldPlayerTestInput>();
            GoldPlayerController pl = tempPlayer.AddComponent <GoldPlayerController>();

            pl.InitOnStart = false;

            Assert.IsFalse(pl.HasBeenFullyInitialized);

            yield return(null);

            yield return(null);

            Assert.IsFalse(pl.HasBeenFullyInitialized);

            Object.DestroyImmediate(tempPlayer);

            tempPlayer = new GameObject();
            tempPlayer.AddComponent <GoldPlayerTestInput>();
            pl = tempPlayer.AddComponent <GoldPlayerController>();
            pl.Camera.TargetCamera = player.Camera.TargetCamera;
            pl.Camera.FieldOfViewKick.EnableFOVKick = false;
            pl.HeadBob.EnableBob = false;
            pl.Camera.CameraHead = tempPlayer.transform;
            pl.InitOnStart       = true;

            yield return(null);

            yield return(null);

            Assert.IsTrue(pl.HasBeenFullyInitialized);

            sceneObjects.Add(tempPlayer);
        }
Beispiel #16
0
        public static void CreateGoldPlayer(MenuCommand menuCommand)
        {
            GameObject parent     = menuCommand.context as GameObject;
            string     uniqueName = GameObjectUtility.GetUniqueNameForSibling(parent != null ? parent.transform : null, "Gold Player Controller");

            // Create the root object.
            GameObject root = new GameObject(uniqueName)
            {
                tag = "Player", layer = LayerMask.NameToLayer("TransparentFX")
            };

            // Place the root in the scene.
            PlaceInScene(root, parent);

            // Create the graphics holder.
            GameObject graphicsHolder = CreateChild("Graphics", root);
            // Create the actual graphic.
            GameObject graphic = GameObject.CreatePrimitive(PrimitiveType.Capsule);

            // Set up the graphic.
            graphic.name  = "Capsule";
            graphic.layer = root.layer;
            graphic.transform.SetParent(graphicsHolder.transform, false);
            graphic.transform.localScale    = new Vector3(0.8f, 1, 0.8f);
            graphic.transform.localPosition = Vector3.up;
            // Remove any colliders, if they are present.
            Collider graphicsCollider = graphic.GetComponent <Collider>();

            if (graphicsCollider != null)
            {
                Object.DestroyImmediate(graphicsCollider);
            }

            // Create the bob target and set the position.
            GameObject bobTarget = CreateChild("Bob Target", root);

            bobTarget.transform.localPosition = new Vector3(0f, 1.6f, 0f);

            // Create the camera head.
            GameObject cameraHead = CreateChild("Camera Head", bobTarget);

            // Create the player camera.
            GameObject playerCameraGo = CreateChild("Player Camera", cameraHead);
            Camera     playerCamera   = playerCameraGo.AddComponent <Camera>();

            playerCamera.clearFlags    = CameraClearFlags.Skybox;
            playerCamera.fieldOfView   = 80;
            playerCamera.nearClipPlane = 0.01f;
            playerCamera.farClipPlane  = 1000f;

            playerCameraGo.AddComponent <FlareLayer>();
            playerCameraGo.AddComponent <AudioListener>();

            // Create the audio object.
            GameObject audioGo = CreateChild("Audio", root);

            AudioSource stepsSource = audioGo.AddComponent <AudioSource>();
            AudioSource jumpSource  = audioGo.AddComponent <AudioSource>();
            AudioSource landSource  = audioGo.AddComponent <AudioSource>();

            // Add the character controller and set it up.
            CharacterController characterController = root.AddComponent <CharacterController>();

            characterController.radius = 0.4f;
            characterController.height = 2;
            characterController.center = new Vector3(0, 1, 0);

            // Add the actual Gold Player Controller and set it up.
            GoldPlayerController goldController = root.AddComponent <GoldPlayerController>();

            goldController.Camera.CameraHead = cameraHead.transform;
            goldController.Camera.FieldOfViewKick.TargetCamera = playerCamera;

            goldController.Movement.GroundLayer = 1;

            goldController.HeadBob.BobTarget = bobTarget.transform;

            goldController.Audio.FootstepsSource = stepsSource;
            goldController.Audio.JumpSource      = jumpSource;
            goldController.Audio.LandSource      = landSource;

#if ENABLE_INPUT_SYSTEM && UNITY_2019_3_OR_NEWER
            root.AddComponent <GoldPlayerInputSystem>();
#else
            root.AddComponent <GoldPlayerInput>();
#endif

            // Register the undo.
            Undo.RegisterCreatedObjectUndo(root, "Create " + root.name);
        }
        private void OnSceneGUI()
        {
            GoldPlayerController controller = target as GoldPlayerController;

            if (controller == null)
            {
                return;
            }

            Color oColor = Handles.color;

            Handles.color = Color.blue;

            Vector3 origin = controller.transform.position + controller.Controller.center;

            Handles.DrawLine(origin, origin + controller.Camera.BodyForward);

            if (GoldPlayerProjectSettings.Instance.ShowGroundCheckGizmos)
            {
                float radius = controller.GetComponent <CharacterController>().radius;

                if (controller.Movement.GroundCheck == GroundCheckType.Raycast)
                {
                    Vector3[] rays = new Vector3[controller.Movement.RayAmount + 1];
                    controller.Movement.CreateGroundCheckRayCircle(ref rays, controller.transform.position, radius);

                    for (int i = 0; i < rays.Length; i++)
                    {
                        if (Application.isPlaying)
                        {
                            bool hit = Physics.Raycast(rays[i], Vector3.down, controller.Movement.RayLength, controller.Movement.GroundLayer, QueryTriggerInteraction.Ignore);
                            Handles.color = hit ? new Color(0f, 1f, 0f, 1f) : new Color(1f, 0f, 0f, 1f);
                        }
                        else
                        {
                            Handles.color = Color.white;
                        }

                        Handles.DrawLine(rays[i], new Vector3(rays[i].x, rays[i].y - controller.Movement.RayLength, rays[i].z));
                    }
                }
                else if (controller.Movement.GroundCheck == GroundCheckType.Sphere)
                {
                    Vector3 pos = new Vector3(controller.transform.position.x, controller.transform.position.y + radius - 0.1f, controller.transform.position.z);
                    if (Application.isPlaying)
                    {
                        Handles.color = controller.Movement.IsGrounded ? new Color(0f, 1f, 0f, 0.25f) : new Color(1f, 0f, 0f, 0.25f);
                    }
                    else
                    {
                        Handles.color = new Color(0f, 1f, 0f, 0.25f);
                    }
                    Handles.SphereHandleCap(0, pos, Quaternion.identity, radius * 2, EventType.Repaint);
                    if (Application.isPlaying)
                    {
                        Handles.color = controller.Movement.IsGrounded ? new Color(0f, 1f, 0f, 1f) : new Color(1f, 0f, 0f, 1f);
                    }
                    else
                    {
                        Handles.color = new Color(0f, 1f, 0f, 1f);
                    }
                    //Handles.DrawWireSphere(pos, radius);
                    Handles.RadiusHandle(Quaternion.identity, pos, radius, false);
                }
            }
            Handles.color = oColor;
        }
        private void OnEnable()
        {
            currentTab = EditorPrefs.GetInt(SELECTED_TAB_PREFS, 0);

            if (currentTab < 0)
            {
                currentTab = 0;
            }

            if (currentTab > 4)
            {
                currentTab = 4;
            }

            camera   = serializedObject.FindProperty("cam");
            movement = serializedObject.FindProperty("movement");
            headBob  = serializedObject.FindProperty("headBob");
            audio    = serializedObject.FindProperty("sounds");

            canLookAround    = camera.FindPropertyRelative("canLookAround");
            shoudLockCursor  = camera.FindPropertyRelative("shouldLockCursor");
            rotateCameraOnly = camera.FindPropertyRelative("rotateCameraOnly");
            invertXAxis      = camera.FindPropertyRelative("invertXAxis");
            invertYAxis      = camera.FindPropertyRelative("invertYAxis");
            lookSensitivity  = camera.FindPropertyRelative("lookSensitivity");
            lookDamping      = camera.FindPropertyRelative("lookDamping");
            minimumX         = camera.FindPropertyRelative("minimumX");
            maximumX         = camera.FindPropertyRelative("maximumX");
            cameraHead       = camera.FindPropertyRelative("cameraHead");
            fieldOfViewKick  = camera.FindPropertyRelative("fieldOfViewKick");

            fovEnable       = fieldOfViewKick.FindPropertyRelative("enableFOVKick");
            fovUnscaledTime = fieldOfViewKick.FindPropertyRelative("unscaledTime");
            fovKickWhen     = fieldOfViewKick.FindPropertyRelative("kickWhen");
            fovKickAmount   = fieldOfViewKick.FindPropertyRelative("kickAmount");
            fovTimeTo       = fieldOfViewKick.FindPropertyRelative("lerpTimeTo");
            fovTimeFrom     = fieldOfViewKick.FindPropertyRelative("lerpTimeFrom");
#if GOLD_PLAYER_CINEMACHINE
            useCinemachine      = fieldOfViewKick.FindPropertyRelative("useCinemachine");
            targetVirtualCamera = fieldOfViewKick.FindPropertyRelative("targetVirtualCamera");
#endif
            targetCamera = fieldOfViewKick.FindPropertyRelative("targetCamera");

            canMoveAround    = movement.FindPropertyRelative("canMoveAround");
            moveUnscaledTime = movement.FindPropertyRelative("unscaledTime");

            walkingSpeed = movement.FindPropertyRelative("walkingSpeeds");

            canRun           = movement.FindPropertyRelative("canRun");
            runToggleMode    = movement.FindPropertyRelative("runToggleMode");
            runSpeeds        = movement.FindPropertyRelative("runSpeeds");
            stamina          = movement.FindPropertyRelative("stamina");
            enableStamina    = stamina.FindPropertyRelative("enableStamina");
            drainStaminaWhen = stamina.FindPropertyRelative("drainStaminaWhen");
            maxStamina       = stamina.FindPropertyRelative("maxStamina");
            drainRate        = stamina.FindPropertyRelative("drainRate");
            stillThreshold   = stamina.FindPropertyRelative("stillThreshold");
            regenRateStill   = stamina.FindPropertyRelative("regenRateStill");
            regenRateMoving  = stamina.FindPropertyRelative("regenRateMoving");
            regenWait        = stamina.FindPropertyRelative("regenWait");

            canCrouch        = movement.FindPropertyRelative("canCrouch");
            crouchToggleMode = movement.FindPropertyRelative("crouchToggleMode");
            crouchSpeeds     = movement.FindPropertyRelative("crouchSpeeds");
            crouchJumping    = movement.FindPropertyRelative("crouchJumping");
            crouchHeight     = movement.FindPropertyRelative("crouchHeight");
            crouchHeadLerp   = movement.FindPropertyRelative("crouchHeadLerp");

            canJump = movement.FindPropertyRelative("canJump");
            jumpingRequiresStamina = movement.FindPropertyRelative("jumpingRequiresStamina");
            jumpStaminaRequire     = movement.FindPropertyRelative("jumpStaminaRequire");
            jumpStaminaCost        = movement.FindPropertyRelative("jumpStaminaCost");
            jumpHeight             = movement.FindPropertyRelative("jumpHeight");
            airJump        = movement.FindPropertyRelative("airJump");
            airJumpTime    = movement.FindPropertyRelative("airJumpTime");
            airJumpsAmount = movement.FindPropertyRelative("airJumpsAmount");
            allowAirJumpDirectionChange = movement.FindPropertyRelative("allowAirJumpDirectionChange");

            movingPlatforms         = movement.FindPropertyRelative("movingPlatforms");
            movingPlatformsEnabled  = movingPlatforms.FindPropertyRelative("enabled");
            movingPlatformsPosition = movingPlatforms.FindPropertyRelative("movePosition");
            movingPlatformsRotation = movingPlatforms.FindPropertyRelative("moveRotation");
            movingPlatformsMaxAngle = movingPlatforms.FindPropertyRelative("maxAngle");

            groundLayer       = movement.FindPropertyRelative("groundLayer");
            acceleration      = movement.FindPropertyRelative("acceleration");
            gravity           = movement.FindPropertyRelative("gravity");
            airControl        = movement.FindPropertyRelative("airControl");
            enableGroundStick = movement.FindPropertyRelative("enableGroundStick");
            groundStick       = movement.FindPropertyRelative("groundStick");
            groundCheck       = movement.FindPropertyRelative("groundCheck");
            rayAmount         = movement.FindPropertyRelative("rayAmount");
            rayHeight         = movement.FindPropertyRelative("rayHeight");
            rayLength         = movement.FindPropertyRelative("rayLength");

            enableBob           = headBob.FindPropertyRelative("bobClass").FindPropertyRelative("enableBob");
            bobUnscaledTime     = headBob.FindPropertyRelative("bobClass").FindPropertyRelative("unscaledTime");
            bobFrequency        = headBob.FindPropertyRelative("bobClass").FindPropertyRelative("bobFrequency");
            bobHeight           = headBob.FindPropertyRelative("bobClass").FindPropertyRelative("bobHeight");
            swayAngle           = headBob.FindPropertyRelative("bobClass").FindPropertyRelative("swayAngle");
            sideMovement        = headBob.FindPropertyRelative("bobClass").FindPropertyRelative("sideMovement");
            heightMultiplier    = headBob.FindPropertyRelative("bobClass").FindPropertyRelative("heightMultiplier");
            strideMultiplier    = headBob.FindPropertyRelative("bobClass").FindPropertyRelative("strideMultiplier");
            landMove            = headBob.FindPropertyRelative("bobClass").FindPropertyRelative("landMove");
            landTilt            = headBob.FindPropertyRelative("bobClass").FindPropertyRelative("landTilt");
            enableStrafeTilting = headBob.FindPropertyRelative("bobClass").FindPropertyRelative("enableStrafeTilting");
            strafeTilt          = headBob.FindPropertyRelative("bobClass").FindPropertyRelative("strafeTilt");
            bobTarget           = headBob.FindPropertyRelative("bobClass").FindPropertyRelative("bobTarget");

            enableAudio       = audio.FindPropertyRelative("enableAudio");
            audioUnscaledTime = audio.FindPropertyRelative("unscaledTime");
            audioType         = audio.FindPropertyRelative("audioType");
            basedOnHeadBob    = audio.FindPropertyRelative("basedOnHeadBob");
            stepTime          = audio.FindPropertyRelative("stepTime");
            walkFootsteps     = audio.FindPropertyRelative("walkFootsteps");
            runFootsteps      = audio.FindPropertyRelative("runFootsteps");
            crouchFootsteps   = audio.FindPropertyRelative("crouchFootsteps");
            jumpingSteps      = audio.FindPropertyRelative("jumping");
            landingSteps      = audio.FindPropertyRelative("landing");
            footstepsSource   = audio.FindPropertyRelative("footstepsSource");
            jumpSource        = audio.FindPropertyRelative("jumpSource");
            landSource        = audio.FindPropertyRelative("landSource");

            goldPlayer          = (GoldPlayerController)target;
            characterController = goldPlayer.GetComponent <CharacterController>();
        }
Beispiel #19
0
 public bool GetButtonDown(string buttonName)
 {
     return(GetButtonDown(GoldPlayerController.InputNameToHash(buttonName)));
 }
        public static void CreateGoldPlayer(MenuCommand menuCommand)
        {
            GameObject parent     = menuCommand.context as GameObject;
            string     uniqueName = GameObjectUtility.GetUniqueNameForSibling(parent != null ? parent.transform : null, "Gold Player Controller");

            // Create the root object.
            GameObject root = new GameObject(uniqueName)
            {
                tag = "Player", layer = LayerMask.NameToLayer("TransparentFX")
            };

            // Place the root in the scene.
            PlaceInScene(root, parent);

            // Create the graphics holder.
            GameObject graphicsHolder = CreateChild("Graphics", root);
            // Create the actual graphic.
            GameObject graphic = GameObject.CreatePrimitive(PrimitiveType.Capsule);

            // Set up the graphic.
            graphic.name  = "Capsule";
            graphic.layer = root.layer;
            graphic.transform.SetParent(graphicsHolder.transform, false);
            graphic.transform.localScale    = new Vector3(0.8f, 1, 0.8f);
            graphic.transform.localPosition = Vector3.up;
            // Remove any colliders, if they are present.
            Collider graphicsCollider = graphic.GetComponent <Collider>();

            if (graphicsCollider != null)
            {
                Object.DestroyImmediate(graphicsCollider);
            }

            // Create the camera head.
            GameObject cameraHead = CreateChild("Camera Head", root);

            cameraHead.transform.localPosition = new Vector3(0f, 1.6f, 0f);

            // Create the bob target and set the position.
            GameObject bobTarget = CreateChild("Bob Target", cameraHead);

            // Create the player camera.
            GameObject playerCameraGo = CreateChild("Player Camera", bobTarget);

#if GOLD_PLAYER_CINEMACHINE
            Cinemachine.CinemachineVirtualCamera playerCamera = playerCameraGo.AddComponent <Cinemachine.CinemachineVirtualCamera>();
            playerCamera.m_Lens.FieldOfView   = 80;
            playerCamera.m_Lens.NearClipPlane = 0.01f;
            playerCamera.m_Lens.FarClipPlane  = 1000f;
#else
            Camera playerCamera = playerCameraGo.AddComponent <Camera>();
            playerCamera.clearFlags    = CameraClearFlags.Skybox;
            playerCamera.fieldOfView   = 80;
            playerCamera.nearClipPlane = 0.01f;
            playerCamera.farClipPlane  = 1000f;
            playerCamera.tag           = "MainCamera";
#endif

            playerCameraGo.AddComponent <FlareLayer>();
            playerCameraGo.AddComponent <AudioListener>();

            // Create the audio object.
            GameObject audioGo = CreateChild("Audio", root);

            AudioSource stepsSource = audioGo.AddComponent <AudioSource>();
            AudioSource jumpSource  = audioGo.AddComponent <AudioSource>();
            AudioSource landSource  = audioGo.AddComponent <AudioSource>();

            // Add the character controller and set it up.
            CharacterController characterController = root.AddComponent <CharacterController>();
            characterController.radius = 0.4f;
            characterController.height = 2;
            characterController.center = new Vector3(0, 1, 0);

            // Add the actual Gold Player Controller and set it up.
            GoldPlayerController goldController = root.AddComponent <GoldPlayerController>();

            goldController.Camera.CameraHead = cameraHead.transform;
#if GOLD_PLAYER_CINEMACHINE
            goldController.Camera.FieldOfViewKick.UseCinemachine      = true;
            goldController.Camera.FieldOfViewKick.TargetVirtualCamera = playerCamera;
#else
            goldController.Camera.FieldOfViewKick.TargetCamera = playerCamera;
#endif

            goldController.Movement.GroundLayer = 1;

            goldController.HeadBob.BobTarget = bobTarget.transform;

            goldController.Audio.FootstepsSource = stepsSource;
            goldController.Audio.JumpSource      = jumpSource;
            goldController.Audio.LandSource      = landSource;

            AudioClip step1 = GetAsset <AudioClip>(STEP1_GUID);
            AudioClip step2 = GetAsset <AudioClip>(STEP2_GUID);
            AudioClip step3 = GetAsset <AudioClip>(STEP3_GUID);
            AudioClip step4 = GetAsset <AudioClip>(STEP4_GUID);
            AudioClip jump  = GetAsset <AudioClip>(JUMP_GUID);
            AudioClip land  = GetAsset <AudioClip>(LAND_GUID);

            AudioItem walkSounds   = CreateAudioItem(true, true, 1f, 0.9f, 1.1f, true, 1f, step1, step2, step3, step4);
            AudioItem runSounds    = CreateAudioItem(true, true, 1.4f, 1.4f, 1.6f, true, 1f, step1, step2, step3, step4);
            AudioItem crouchSounds = CreateAudioItem(true, true, 1f, 0.9f, 1.1f, true, 0.4f, step1, step2, step3, step4);
            AudioItem jumpSound    = CreateAudioItem(true, true, 1f, 0.9f, 1.1f, true, 1f, jump);
            AudioItem landSound    = CreateAudioItem(true, true, 1f, 0.9f, 1.1f, true, 1f, land);

            goldController.Audio.WalkFootsteps   = walkSounds;
            goldController.Audio.RunFootsteps    = runSounds;
            goldController.Audio.CrouchFootsteps = crouchSounds;
            goldController.Audio.Jumping         = jumpSound;
            goldController.Audio.Landing         = landSound;

#if ENABLE_INPUT_SYSTEM && GOLD_PLAYER_NEW_INPUT
            root.AddComponent <GoldPlayerInputSystem>();
#else
            root.AddComponent <GoldPlayerInput>();
#endif

            // Register the undo.
            Undo.RegisterCreatedObjectUndo(root, "Create " + root.name);
        }