FPS project footsteps-Unity notes (2021.1.22)

What we achieved today:

To make footsteps, I learned the principle of ScriptableObject. Script sound resources use ScriptableObject to store applications. When the character touches the ground and is shifting, play footsteps.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "FPS/Footstep Audio Data")]
public class FootstepAudioData : ScriptableObject
{
    
    
    public List<FootstepAudio> footstepAudios = new List<FootstepAudio>();
}

[System.Serializable]
public class FootstepAudio
{
    
    
    // 用于标记踩在什么表面上
    public string Tag;
    // 声音片段列表
    public List<AudioClip> AudioClips = new List<AudioClip>();
    // 声音间隔
    public float walkVoiceInterval;
    //public float sprintingVoiceInterval;
    //public float CrouchingVoiceInterval;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerFootstepListener : MonoBehaviour
{
    
    
    public FootstepAudioData footstepAudioData;
    public AudioSource footstepAudioSource;

    private CharacterController characterController;
    private Transform footstepTransform;
    private float timePassed = 0;
    private FPCharacterControllerMovement movement;

    // 角色移动或者发生较大幅度位移时 播放脚步声
    // 根据角色踩踏的不同材质播放不同的声音
    // 具体实现方式采用Physics API

    private void Start()
    {
    
    
        characterController = GetComponent<CharacterController>();
        footstepTransform = transform;
        movement = GetComponent<FPCharacterControllerMovement>();
    }

    private void FixedUpdate()
    {
    
    
        // 角色处于地面
        if(characterController.isGrounded)
        {
    
    
            // 角色正在移动
            if(characterController.velocity.normalized.sqrMagnitude > 0.1f)
            {
    
    
                // 设置下一次播放的时间
                timePassed += Time.deltaTime;

                // 播放移动声音
                if(Physics.Linecast(footstepTransform.position,
                    footstepTransform.position + Vector3.down * characterController.height,
                    out RaycastHit temp_hitInfo)) //检测当前踩踏在什么地面上
                {
    
    
                    #if UNITY_EDITOR
                    Debug.Log(temp_hitInfo.collider.tag);
                    Debug.DrawRay(footstepTransform.position,
                    -footstepTransform.up * characterController.height, Color.red);
                    #endif

                    // 对于所有类型的脚步声
                    foreach (var temp_audioElement in footstepAudioData.footstepAudios)
                    {
    
    
                        // 找到匹配的类型
                        if (temp_hitInfo.collider.CompareTag(temp_audioElement.Tag))
                        {
    
    
                            // 用于保存实际要用的声音播放间隔
                            float temp_VoiceInterval = 0;
                            if (movement.m_isSprinting && movement.m_isCrouched) //是否在蹲下时冲刺
                            {
    
    
                                temp_VoiceInterval = temp_audioElement.walkVoiceInterval;
                            }
                            else if(movement.m_isCrouched) //是否蹲下
                            {
    
    
                                temp_VoiceInterval = temp_audioElement.walkVoiceInterval * 2;
                            }
                            else if (movement.m_isSprinting) //是否在冲刺
                            {
    
    
                                temp_VoiceInterval = temp_audioElement.walkVoiceInterval / 2;
                            }
                            else // 那就只是在走了
                            {
    
    
                                temp_VoiceInterval = temp_audioElement.walkVoiceInterval;
                            }
                            Debug.Log(temp_VoiceInterval);
                            // 如果到了下一次播放时间
                            if (timePassed >= temp_VoiceInterval)
                            {
    
    
                                // 获取这个类型脚步声的素材总数
                                int temp_audioCount = temp_audioElement.AudioClips.Count;
                                // 随机获取一个
                                int temp_audioIndex = UnityEngine.Random.Range(0, temp_audioCount);
                                // 获取到对应的声音片段
                                AudioClip temp_footstepAudioClip = temp_audioElement.AudioClips[temp_audioIndex];
                                // 播放音效
                                footstepAudioSource.clip = temp_footstepAudioClip;
                                footstepAudioSource.Play();
                                timePassed = 0;
                                // 找到了就行了 直接跳出循环
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

}

FootstepAudioData is responsible for generating the ScriptableObject used to store the sound effect resources, and the PlayerFootstepListener is used to judge the ground where the character is located, find the corresponding sound effect, randomly obtain the sound effect, and play the sound effect. The sound effect has a playback interval, to barely achieve the effect that sounds real, and it also realizes that different motion states correspond to different sound effect playback intervals.

Effect: footsteps and BUG when moving


BUG and defects:

Sometimes the tags on the ground cannot be detected inexplicably. But in fact, my ray detection is wrong, the end point of the detection should be the point where the character goes down, not the origin point down. The above code is corrected to be correct.
There are no footsteps when squatting down. It should be that the ray cannot detect the ground.
The biggest flaw, this sound effect is really ugly, forget it, just learn how to implement the function.


worth taking note of:

How to judge the mobile status? I directly quoted the previous movement control script. The script has a bool value describing the movement state, which is different from what the teacher did.


Guess you like

Origin blog.csdn.net/qq_37856544/article/details/112993447