private void Update_LookingForTarget()
    {
        if (!agent.isOnNavMesh)
        {
            // If agent has fallen off the nav mesh, move forward and attempt to get back on the navmesh.
            var velocity = transform.forward;
            var rotation = Quaternion.LookRotation(velocity, Vector3.up);
            var speed    = sprintNext ? MovementSpeed.Sprint : MovementSpeed.Run;
            movementDriver.ApplyMovement(velocity, rotation, speed, jumpNext);
            jumpNext = false;
            agent.Warp(transform.position);
        }
        else if (agent.remainingDistance < MinRemainingDistance || agent.pathStatus == NavMeshPathStatus.PathInvalid ||
                 !agent.hasPath)
        {
            SetRandomDestination();
        }
        else if (agent.pathStatus == NavMeshPathStatus.PathComplete)
        {
            var velocity = agent.desiredVelocity;
            velocity.y = 0;
            if (velocity != Vector3.zero)
            {
                var rotation = Quaternion.LookRotation(velocity, Vector3.up);
                var speed    = sprintNext ? MovementSpeed.Sprint : MovementSpeed.Run;
                movementDriver.ApplyMovement(velocity, rotation, speed, jumpNext);
                jumpNext = false;
            }
        }

        agent.nextPosition = transform.position;
    }
Exemple #2
0
    private void MoveTowards(Vector3 destination, MovementSpeed speed, Quaternion rotation, bool jump = false)
    {
        var agentPosition   = agent.nextPosition;
        var direction       = (destination - agentPosition).normalized;
        var desiredVelocity = direction * movementDriver.GetSpeed(speed);

        // Setting nextPosition will move the agent towards destination, but is constrained by the navmesh
        agent.nextPosition += desiredVelocity * Time.deltaTime;

        // Getting nextPosition here will return the constrained position of the agent
        var actualDirection = (agent.nextPosition - agentPosition).normalized;

        movementDriver.ApplyMovement(actualDirection, rotation, speed, jump);
    }
        private void Update()
        {
            if (controller.MenuPressed)
            {
                ClientWorkerHandler.ScreenUIController.TryOpenSettingsMenu();
            }

            // Don't allow controls if in the menu.
            if (ScreenUIController.InEscapeMenu)
            {
                // Still apply physics.
                movement.ApplyMovement(Vector3.zero, transform.rotation, MovementSpeed.Run, false);
                Animations(false);
                return;
            }

            if (isRequestingRespawn)
            {
                return;
            }

            if (health.Data.Health == 0)
            {
                if (controller.RespawnPressed)
                {
                    isRequestingRespawn        = true;
                    requestingRespawnCoroutine = StartCoroutine(RequestRespawn());
                }

                return;
            }

            // Movement
            var toMove = transform.rotation * controller.Movement;

            // Rotation
            var yawDelta   = controller.YawDelta;
            var pitchDelta = controller.PitchDelta;

            // Modifiers
            var isAiming    = controller.IsAiming;
            var isSprinting = controller.AreSprinting;

            var isJumpPressed = controller.JumpPressed;

            // Events
            var shootPressed = controller.ShootPressed;
            var shootHeld    = controller.ShootHeld;


            // Update the pitch speed with that of the gun if aiming.
            var yawSpeed   = cameraSettings.YawSpeed;
            var pitchSpeed = cameraSettings.PitchSpeed;

            if (isAiming)
            {
                yawSpeed   = currentGun.CurrentGunSettings.AimYawSpeed;
                pitchSpeed = currentGun.CurrentGunSettings.AimPitchSpeed;
            }

            //Mediator
            var movementSpeed = isAiming
                ? MovementSpeed.Walk
                : isSprinting
                    ? MovementSpeed.Sprint
                    : MovementSpeed.Run;
            var yawChange    = yawDelta * yawSpeed;
            var pitchChange  = pitchDelta * -pitchSpeed;
            var currentPitch = pitchTransform.transform.localEulerAngles.x;
            var newPitch     = currentPitch + pitchChange;

            if (newPitch > 180)
            {
                newPitch -= 360;
            }

            newPitch = Mathf.Clamp(newPitch, -cameraSettings.MaxPitch, -cameraSettings.MinPitch);
            pitchTransform.localRotation = Quaternion.Euler(newPitch, 0, 0);
            var currentYaw = transform.eulerAngles.y;
            var newYaw     = currentYaw + yawChange;
            var rotation   = Quaternion.Euler(newPitch, newYaw, 0);

            //Check for sprint cooldown
            if (!movement.HasSprintedRecently)
            {
                HandleShooting(shootPressed, shootHeld);
            }

            Aiming(isAiming);

            var wasGroundedBeforeMovement = movement.IsGrounded;

            movement.ApplyMovement(toMove, rotation, movementSpeed, isJumpPressed);
            Animations(isJumpPressed && wasGroundedBeforeMovement);
        }
        private void Update()
        {
            // Don't allow controls if in the menu.
            if (ScreenUIController.InEscapeMenu)
            {
                // Still apply physics.
                movement.ApplyMovement(Vector3.zero, transform.rotation, MovementSpeed.Run, false);
                Animations(false);
                return;
            }

            if (isRequestingRespawn)
            {
                return;
            }

            if (health.Data.Health == 0)
            {
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    isRequestingRespawn        = true;
                    requestingRespawnCoroutine = StartCoroutine(RequestRespawn());
                }

                return;
            }

            // Movement
            var forward  = Input.GetKey(KeyCode.W);
            var backward = Input.GetKey(KeyCode.S);
            var left     = Input.GetKey(KeyCode.A);
            var right    = Input.GetKey(KeyCode.D);

            var toMove      = transform.rotation * GetDirectionFromCache(forward, backward, left, right);
            var onlyForward = forward && !(backward || left || right);

            // Rotation
            var yawDelta   = Input.GetAxis("Mouse X");
            var pitchDelta = Input.GetAxis("Mouse Y");

            // Modifiers
            var isAiming      = Input.GetMouseButton(1);
            var isSprinting   = Input.GetKey(KeyCode.LeftShift) && onlyForward;
            var isJumpPressed = Input.GetKeyDown(KeyCode.Space);

            // Events
            var shootPressed = Input.GetMouseButtonDown(0);
            var shootHeld    = Input.GetMouseButton(0);

            // Update the pitch speed with that of the gun if aiming.
            var yawSpeed   = cameraSettings.YawSpeed;
            var pitchSpeed = cameraSettings.PitchSpeed;

            if (isAiming)
            {
                yawSpeed   = currentGun.CurrentGunSettings.AimYawSpeed;
                pitchSpeed = currentGun.CurrentGunSettings.AimPitchSpeed;
            }

            //Mediator
            var movementSpeed = isAiming
                ? MovementSpeed.Walk
                : isSprinting
                    ? MovementSpeed.Sprint
                    : MovementSpeed.Run;
            var yawChange    = yawDelta * yawSpeed;
            var pitchChange  = pitchDelta * -pitchSpeed;
            var currentPitch = pitchTransform.transform.localEulerAngles.x;
            var newPitch     = currentPitch + pitchChange;

            if (newPitch > 180)
            {
                newPitch -= 360;
            }

            newPitch = Mathf.Clamp(newPitch, -cameraSettings.MaxPitch, -cameraSettings.MinPitch);
            pitchTransform.localRotation = Quaternion.Euler(newPitch, 0, 0);
            var currentYaw = transform.eulerAngles.y;
            var newYaw     = currentYaw + yawChange;
            var rotation   = Quaternion.Euler(newPitch, newYaw, 0);

            //Check for sprint cooldown
            if (!movement.HasSprintedRecently)
            {
                HandleShooting(shootPressed, shootHeld);
            }

            Aiming(isAiming);

            var wasGroundedBeforeMovement = movement.IsGrounded;

            movement.ApplyMovement(toMove, rotation, movementSpeed, isJumpPressed);
            Animations(isJumpPressed && wasGroundedBeforeMovement);
        }