// ------------------------------------------------------------------------- // 方法 : FixedUpdate // 介绍 : 用于获取按键和调整方向 // ------------------------------------------------------------------------- protected void FixedUpdate() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); bool waswalking = _isWalking; _isWalking = !Input.GetKey(KeyCode.LeftShift); float speed = _isCrouching ? _crouchSpeed : _isWalking ? _walkSpeed : _runSpeed; _inputVector = new Vector2(horizontal, vertical); if (_inputVector.sqrMagnitude > 1) { _inputVector.Normalize(); } Vector3 desiredMove = transform.forward * _inputVector.y + transform.right * _inputVector.x; RaycastHit hitInfo; if (Physics.SphereCast(transform.position, _characterController.radius, Vector3.down, out hitInfo, _characterController.height / 2f, 1)) { desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized; } _moveDirection.x = desiredMove.x * speed; _moveDirection.z = desiredMove.z * speed; if (_characterController.isGrounded) { _moveDirection.y = -_stickToGroundForce; if (_jumpButtonPressed) { _moveDirection.y = _jumpSpeed; _jumpButtonPressed = false; _isJumping = true; //TODO:跳跃音效 } } else { _moveDirection += Physics.gravity * _gravityMultiplier * Time.fixedDeltaTime; } _characterController.Move(_moveDirection * Time.fixedDeltaTime); Vector3 speedXZ = new Vector3(_characterController.velocity.x, 0.0f, _characterController.velocity.z); if (speedXZ.magnitude > 0.01f) { _camera.transform.localPosition = _localSpaceCameraPos + _headBob.GetVectorOffset(speedXZ.magnitude * (_isCrouching || _isWalking?1.0f:_runStepLengthen)); } else { _camera.transform.localPosition = _localSpaceCameraPos; } }
void FixedUpdate() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); bool wasWalking = _isWalking; _isWalking = !Input.GetKey(KeyCode.LeftShift); float speed = _isCrouching ? _crouchSpeed : _isWalking ? _walkSpeed : Mathf.Lerp(_walkSpeed, _runSpeed, _stamina.value / 100); _inputVector = new Vector2(horizontal, vertical); if (_inputVector.magnitude > 1) { _inputVector.Normalize(); } Vector3 desiredMove = transform.forward * _inputVector.y + transform.right * _inputVector.x; RaycastHit hitInfo; if (Physics.SphereCast(transform.position, _characterController.radius, Vector3.down, out hitInfo, _characterController.height / 2f, 1)) { // This means we are standing on a surface desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized; } _moveDirection.x = !_freezeMovement ? desiredMove.x * speed * _dragMultiplier : 0f; _moveDirection.z = !_freezeMovement ? desiredMove.z * speed * _dragMultiplier : 0f; if (_characterController.isGrounded) { if (_jumpButtonPressed) { _moveDirection.y = _jumpSpeed; // We reset the jump button to fase so in another update function, it could be set to true again _jumpButtonPressed = false; // TODO: Play Jumping sound } } else { _moveDirection += Physics.gravity * _gravityMultiplier * Time.fixedDeltaTime; } _characterController.Move(_moveDirection * Time.fixedDeltaTime); // Camera bob // Nullifying the speed relative to the Y axis Vector3 speedXZ = new Vector3(_characterController.velocity.x, 0f, _characterController.velocity.z); if (speedXZ.magnitude > 0.01f) { _camera.transform.localPosition = _localSpaceCameraPos + _headBob.GetVectorOffset(speedXZ.magnitude * (_isWalking || _isCrouching ? 1 : _runStepLengthen)); } else { _camera.transform.localPosition = _localSpaceCameraPos; } // Update broadcasters if (_broadcastPosition != null) { _broadcastPosition.value = transform.position; } if (_broadcastDirection != null) { _broadcastDirection.value = transform.forward; } }
protected void FixedUpdate() { // Read input from axis float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); bool waswalking = _isWalking; _isWalking = !Input.GetKey(KeyCode.LeftShift); // Set the desired speed to be either our walking speed or our running speed float speed = _isCrouching ? _crouchSpeed : _isWalking ? _walkSpeed : _runSpeed; _inputVector = new Vector2(horizontal, vertical); // normalize input if it exceeds 1 in combined length: if (_inputVector.sqrMagnitude > 1) { _inputVector.Normalize(); } // Always move along the camera forward as it is the direction that it being aimed at Vector3 desiredMove = transform.forward * _inputVector.y + transform.right * _inputVector.x; // Get a normal for the surface that is being touched to move along it RaycastHit hitInfo; if (Physics.SphereCast(transform.position, _characterController.radius, Vector3.down, out hitInfo, _characterController.height / 2f, 1)) { desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized; } // Scale movement by our current speed (walking value or running value) _moveDirection.x = desiredMove.x * speed; _moveDirection.z = desiredMove.z * speed; // If grounded if (_characterController.isGrounded) { // Apply severe down force to keep control sticking to floor _moveDirection.y = -_stickToGroundForce; // If the jump button was pressed then apply speed in up direction // and set isJumping to true. Also, reset jump button status if (_jumpButtonPressed) { _moveDirection.y = _jumpSpeed; _jumpButtonPressed = false; _isJumping = true; // TODO: Play Jumping Sound } } else { // Otherwise we are not on the ground so apply standard system gravity multiplied // by our gravity modifier _moveDirection += Physics.gravity * _gravityMultiplier * Time.fixedDeltaTime; } // Move the Character Controller _characterController.Move(_moveDirection * Time.fixedDeltaTime); // Are we moving Vector3 speedXZ = new Vector3(_characterController.velocity.x, 0.0f, _characterController.velocity.z); if (speedXZ.magnitude > 0.01f) { _camera.transform.localPosition = _localSpaceCameraPos + _headBob.GetVectorOffset(speedXZ.magnitude * (_isCrouching || _isWalking ? 1.0f : _runStepLengthen)); } else { _camera.transform.localPosition = _localSpaceCameraPos; } }
/// <summary> /// Move direction vector accuratley describes the direction the player wants to move the controler even when player is not grounded /// it is a downward motion of player falling under gravity. /// </summary> protected void FixedUpdate() { if (Time.timeScale != 0) { // Read input from axis float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); bool waswalking = isWalking; isWalking = !Input.GetKey(KeyCode.LeftShift); //see if left shift is being held // uses quick if/else to set the speed to either walking or running speed float speed = isCrouching ? crouchingSpeed : isWalking ? walkingSpeed : runningSpeed; inputVector = new Vector2(horizontal, vertical); //inputVector to be multiplied by the speed and used to move player through world. If > 1 then it will move faster //than walkspeed or runspeed if (inputVector.sqrMagnitude > 1) { inputVector.Normalize(); } //take inputVector, then calculate 3D vector that describes direction we want to move // Always move along the camera forward as it is the direction that it being aimed at Vector3 desiredMove = transform.forward * inputVector.y + transform.right * inputVector.x; // Get a normal for the surface that is being touched to move along it //if Spherecast returns true then standing on surface, need to project desiredmovement onto the plane RaycastHit hitInfo; //amount want to cast ray down if (Physics.SphereCast(transform.position, characterController.radius, Vector3.down, out hitInfo, characterController.height / 2f, 1)) { desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized; } //have a normalized vector that describes accuratley the direction player wants to move //now describes amount we want to move vertically and horizontally moveDirection.x = desiredMove.x * speed; moveDirection.z = desiredMove.z * speed; //Check if going up or down // If grounded if (characterController.isGrounded) { // Apply large down force to keep controller sticking to floor moveDirection.y = -stickingToGroundForce; // If the jump button was pressed then add speed in upwards // then set isJumping to true and reset jump button status if (jumpButtonPressed) { moveDirection.y = jumpingSpeed; jumpButtonPressed = false; isJumping = true; } } else { // Otherwise we are not on the ground so apply standard system gravity multiplied // by our gravity modifier // If not grounded then controller is in air and falling so add current gravity force * the gravity multiplier moveDirection += Physics.gravity * gravityMultiplier * Time.fixedDeltaTime; } // Move the Character Controller characterController.Move(moveDirection * Time.fixedDeltaTime); // Are we moving //When we run the headbob wont be quite as fast Vector3 speedXZ = new Vector3(characterController.velocity.x, 0.0f, characterController.velocity.z); //zero out any up and down movement if (speedXZ.magnitude > 0.01f) { _camera.transform.localPosition = localSpaceCameraPos + headBob.GetVectorOffset(speedXZ.magnitude * (isCrouching || isWalking ? 1.0f : runStepLengthen)); } else { _camera.transform.localPosition = localSpaceCameraPos; } } }
protected void FixedUpdate() { // Lettura input da axis float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); bool waswalking = _isWalking; _isWalking = !Input.GetKey(KeyCode.LeftShift); // Setta la velocità del Player in base a se si sta camminando o correndo float speed = _isCrouching ? _crouchSpeed : _isWalking ? _walkSpeed : _runSpeed; _inputVector = new Vector2(horizontal, vertical); // Normalizza l'input nel caso in cui si dovesse sforare l'1 if (_inputVector.sqrMagnitude > 1) { _inputVector.Normalize(); } // Si muove sempre frontalmente in base alla telecamera Vector3 desiredMove = transform.forward * _inputVector.y + transform.right * _inputVector.x; RaycastHit hitInfo; if (Physics.SphereCast(transform.position, _characterController.radius, Vector3.down, out hitInfo, _characterController.height / 2f, 1)) { desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized; } // Scaling del movimento in base alla velocità attuale _moveDirection.x = desiredMove.x * speed * _dragMultiplier; _moveDirection.z = desiredMove.z * speed * _dragMultiplier; if (_characterController.isGrounded) { // Applico una forza verso il basso in modo da riomanere attaccato al pavimento _moveDirection.y = -_stickToGroundForce; // Se il Jump Button è stato premuto applica la speed verso l'alto // e setta isJumping a true + ripristina lo sato del pulsante Jump if (_jumpButtonPressed) { _moveDirection.y = _jumpSpeed; _jumpButtonPressed = false; _isJumping = true; // TODO: Play Jumping Sound } } else { // Se non siamo a terra applica la gravità di Unity + modificatore _moveDirection += Physics.gravity * _gravityMultiplier * Time.fixedDeltaTime; } // Muove il Character Controller _characterController.Move(_moveDirection * Time.fixedDeltaTime); Vector3 speedXZ = new Vector3(_characterController.velocity.x, 0.0f, _characterController.velocity.z); if (speedXZ.magnitude > 0.01f) { _camera.transform.localPosition = _localSpaceCameraPos + _headBob.GetVectorOffset(speedXZ.magnitude * (_isCrouching || _isWalking ? 1.0f : _runStepLengthen)); } else { _camera.transform.localPosition = _localSpaceCameraPos; } }
protected void FixedUpdate() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); _isWalking = !Input.GetKey(KeyCode.LeftShift); float speed = _isCrouching ? _crouchSpeed : _isWalking ? _walkSpeed : _runSpeed; _inputVector = new Vector2(horizontal, vertical); if (_inputVector.sqrMagnitude > 1) { _inputVector.Normalize(); } Vector3 desiredMove = transform.forward * _inputVector.y + transform.right * _inputVector.x; // Get a normal for the surface that is being touched to move along it if (Physics.SphereCast(transform.position, characterController.radius, Vector3.down, out var hitInfo, characterController.height / 2f, 1)) { desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized; } // Scale movement by our current speed (walking value or running value) _moveDirection.x = desiredMove.x * speed; _moveDirection.z = desiredMove.z * speed; // If grounded if (characterController.isGrounded) { // Apply severe down force to keep control sticking to floor _moveDirection.y = -_stickToGroundForce; // If the jump button was pressed then apply speed in up direction // and set isJumping to true. Also, reset jump button status if (_jumpButtonPressed) { _moveDirection.y = _jumpSpeed; _jumpButtonPressed = false; _isJumping = true; } } else { // Otherwise we are not on the ground so apply standard system gravity multiplied // by our gravity modifier _moveDirection += Physics.gravity * _gravityMultiplier * Time.fixedDeltaTime; } characterController.Move(_moveDirection * Time.fixedDeltaTime); Vector3 speedXz = new Vector3(characterController.velocity.x, 0, characterController.velocity.z); if (speedXz.magnitude > 0.01f && characterController.isGrounded) { _camera.transform.localPosition = _localSpaceCameraPos + _headBob.GetVectorOffset(characterController.velocity.magnitude * (_isCrouching || _isWalking ? 1.0f : _runStepLengthen)); } else { _camera.transform.localPosition = _localSpaceCameraPos; } // Update broadcasters _broadcastPosition.value = transform.position; _broadcastDirection.value = transform.forward; }