public override void OnInspectorGUI() { ScriptableAudioEvent scriptableAudioEvent = (ScriptableAudioEvent)target; serializedObject.Update(); EditorGUILayout.PropertyField(_clipsProperty, true); EditorGUILayout.PropertyField(_randomizeVolumeProperty); if (scriptableAudioEvent.RandomizeVolume) { EditorGUILayout.PropertyField(minMaxVolumeProperty); } else { EditorGUILayout.PropertyField(_volumeProperty); } EditorGUILayout.PropertyField(_randomizePitchProperty); if (scriptableAudioEvent.RandomizePitch) { EditorGUILayout.PropertyField(minMaxPitchProperty); } else { EditorGUILayout.PropertyField(_pitchProperty); } serializedObject.ApplyModifiedProperties(); if (GUILayout.Button("Preview SFX")) { scriptableAudioEvent.Play(_previewAudioSource); } }
// Called as an animation event private void PlayAudioEvent(AnimationEvent animEvent) { ScriptableAudioEvent audioEvent = animEvent.objectReferenceParameter as ScriptableAudioEvent; if (audioEvent != null) { audioEvent.Play(_audioSource); } }
public void Collect(Inventory inventory) { // Add item to inventory bool wasPickedUp = inventory.TryAddItem(_itemToPickup); // Destroy the pickup once the object has been successfully picked up if (wasPickedUp) { // Play the pickup SFX when picking up the item _pickupSFX.Play(_audioSource); Destroy(gameObject); } }
/// <summary> /// Uses the item and executes its effect. /// </summary> public virtual void UseItem(AudioSource audioSource) { OnItemUsed?.Invoke(this); //if there is an audio source, try playing a sound. if (audioSource) { //attempt using the unique sound. if (useSound) { useSound.Play(audioSource); return; } //otherwise try using the item type's fallback use sound. var audio = _itemType.FallbackUseSound; if (audio) { audio.Play(audioSource); } } }
//animation event. void PlaySound(ScriptableAudioEvent sound) { sound.Play(_audioSource); }
private void Update() { // Do not take any input into account, if input is blocked if (BlockInput) { return; } // Fetch inputs // GetAxisRaw : -1, +1 (0) // GetAxis: [-1, +1] float horizontalInput = Input.GetAxisRaw("Horizontal"); float verticalInput = Input.GetAxisRaw("Vertical"); bool jumpDown = Input.GetButtonDown("Jump"); bool isSprinting = Input.GetButton("Sprint"); bool isDashing = Input.GetButtonDown("Dash"); // Calculate a direction from input data Vector3 direction = new Vector3(horizontalInput, 0, verticalInput).normalized; // If the player has given any input, adjust the character rotation if (direction != Vector3.zero) { float lookRotationAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + _cameraTransform.eulerAngles.y; Quaternion targetRotation = Quaternion.Euler(0, lookRotationAngle, 0); _graphicsObject.rotation = Quaternion.Slerp(_graphicsObject.rotation, targetRotation, GetSmoothTimeAfterAirControl(_turnSmoothTime, false)); } // Calculate velocity based on gravity formula: delta-y = 1/2 * g * t^2 // We ignore the 1/2 to safe multiplications and because it feels better. // Second Time.deltaTime is done in controller.Move()-call so we save one multiplication here. _currentVerticalVelocity += Physics.gravity.y * _gravityModifier * Time.deltaTime; // Clamp velocity to reach no more than our defined terminal velocity _currentVerticalVelocity = Mathf.Clamp(_currentVerticalVelocity, -_terminalVelocity, JumpVelocity); // Calculate velocity vector based on gravity and speed // (0, 0, z) -> (0, y, z) float targetSpeed = (isSprinting ? _sprintSpeed : _movementSpeed) * direction.magnitude; _currentForwardVelocity = Mathf.SmoothDamp(_currentForwardVelocity, targetSpeed, ref _speedSmoothVelocity, GetSmoothTimeAfterAirControl(_speedSmoothTime, true)); // If dash was pressed, dash. If we don't want to allow dash while air-borne, ask if grounded. if (isDashing) { // Either set velocity = DashVelocity or add it. Both gives a nice small dash effect. _currentForwardVelocity += DashVelocity; } Vector3 velocity = _graphicsObject.forward * _currentForwardVelocity + Vector3.up * _currentVerticalVelocity; // Use the direction to move the character controller // direction.x * Time.deltaTime, direction.y * Time.deltaTime, ... -> resultingDirection.x * _movementSpeed // Time.deltaTime * _movementSpeed = res, res * direction.x, res * direction.y, ... _characterController.Move(velocity * Time.deltaTime); // Check if we are grounded, if so reset gravity _isGrounded = Physics.CheckSphere(_groundCheckTransform.position, _groundCheckRadius, _groundCheckLayerMask); _playerAnimationHandler.SetGrounded(_isGrounded); if (_isGrounded) { // Reset current vertical velocity _currentVerticalVelocity = 0f; } if (_wasGroundedLastFrame && !_isGrounded) { // Play jump sound _jumpAudioEvent.Play(_audioSource); } else if (!_wasGroundedLastFrame && _isGrounded) { // Play landing sound _landAudioEvent.Play(_audioSource); } // If we are grounded and jump was pressed, jump if (_isGrounded && jumpDown) { _playerAnimationHandler.DoJump(); _currentVerticalVelocity = JumpVelocity; } _playerAnimationHandler.SetSpeeds(_currentForwardVelocity, _currentVerticalVelocity); _wasGroundedLastFrame = _isGrounded; }
// Called as an animation event private void DoFootStepSound() { _footstepAudioEvent.Play(_audioSource); }