[Realize 18 of 100 Games with Unity] Create a CSGO/CS2, CF first-person FPS shooting game from scratch - Basics 3 (with project source code)

The final effect of this section

Insert image description here

Preface

This section mainly implements adding sound effects, and some special effects and weapon swing adjustments.

material

material, for convenience, I directly used unity’s free sound effect output, and will use some of its special effects later
https://assetstore.unity.com/packages/templates /systems/low-poly-shooter-pack-free-sample-144839
Insert image description here

Character movement sound effects

Modify PlayerController

MoveSound();

//移动音效
public void MoveSound()
{
    
    
    // 如果在地面上并且移动长度大于0.9
    if (isGround && moveDirection.sqrMagnitude > 0.9f)
    {
    
    
        audioSource.clip = isRun ? runingSound : walkingSound;
        if (!audioSource.isPlaying) audioSource.Play();
    }
    else
    {
    
    
        if (audioSource.isPlaying) audioSource.Pause();
    }
}

Configuration parameters
Insert image description here
Test effect, there is no sound in the screenshot, so it will not be demonstrated here
Insert image description here

Muzzle flash and firing sound effects

Modify WeaponController

[Header("特效")]
public ParticleSystem muzzleFlash;//枪口火焰特效
[Header("声音")]
private AudioSource audioSource;
public AudioClip fireSound;

private void Start()
{
    
    
    currentBullects = bulletsMag;
    audioSource = GetComponent<AudioSource>();
}

// 射击
public void GunFire()
{
    
    
    //。。。

    PlayerShootSound();
    muzzleFlash.Play();//枪口火焰
}


//播放射击音效
public void PlayerShootSound()
{
    
    
    audioSource.clip = fireSound;
    audioSource.Play();
}

Configure parameters and place special effects at the muzzle
Insert image description here
I changed the parameters slightly for the flame particle effects
Insert image description here

Effect
Insert image description here

muzzle light

Lighting introduction:Zero foundation will take you from novice to super god 14 - the production of lights, cameras, sky boxes and mirrors

Add a point light source at the muzzle, configure parameters, and turn off the light by default
Insert image description here
Modify WeaponController

public Light muzzleFlashLight;//枪口火焰灯光

void Update()
{
    
    
    if (Input.GetMouseButton(0) && currentBullects > 0)
    {
    
    
        GunFire();
    }
    else
    {
    
    
        muzzleFlashLight.enabled = false;
    }
}

// 射击
public void GunFire()
{
    
    
    //...
    muzzleFlashLight.enabled = true;//枪口灯光
}

Effect
Insert image description here

bullet holes and sparks

In order to save trouble, I directly separated the special effects of P_IMP_Concrete
Insert image description here
Modify WeaponController

public GameObject hitParticle;//子弹击中火花粒子特效
public GameObject bullectHole;//弹孔

// 射击
public void GunFire()
{
    
    
    if (fireTimer < fireRate || currentBullects <= 0) return;
    isFire = true;
    RaycastHit hit;
    Vector3 shootDirection = shooterPoint.forward; // 射击方向(向前)

    //场景显示红线,方便调试查看
    Debug.DrawRay(shooterPoint.position, shooterPoint.position + shootDirection * range, Color.red);

    if (Physics.Raycast(shooterPoint.position, shootDirection, out hit, range)) // 判断射击
    {
    
    
        Debug.Log(hit.transform.name + "被击中了");
        GameObject hitParticleEffect = Instantiate(hitParticle, hit.point, Quaternion.identity);//实例出击中特效
        GameObject bullectHoleEffect=Instantiate (bullectHole, hit.point,Quaternion.FromToRotation(Vector3.up,hit.normal));//实例出弹孔号
        Destroy(hitParticleEffect, 1f);
        Destroy(bullectHoleEffect, 3f);
    }
    currentBullects--;
    fireTimer = 0;

    PlayerShootSound();
}

Configuration parameters
Insert image description here
Added ImpactScript script, which is used to control the effect when the projectile hits the surface, play the impact sound effect, and destroy it after a certain period of time

public class ImpactScript : MonoBehaviour
{
    
    
	[Header("持续时间")]
	public float despawnTimer = 10.0f;

	[Header("音效")]
	public AudioClip[] impactSounds;
	public AudioSource audioSource;

	private void Start()
	{
    
    
		// 启动销毁计时器
		StartCoroutine(DespawnTimer());

		// 从数组中随机选择一个音效剪辑
		audioSource.clip = impactSounds[Random.Range(0, impactSounds.Length)];
		// 播放随机音效
		audioSource.Play();
	}

	private IEnumerator DespawnTimer()
	{
    
    
		// 等待指定时间
		yield return new WaitForSeconds(despawnTimer);
		// 销毁撞击效果物体
		Destroy(gameObject);
	}
}

Mount configuration parameters
Insert image description here

Effect
Insert image description here

Add the effect of the weapon swinging with the camera arm

New

// 武器摇摆
public class WeaponSway : MonoBehaviour
{
    
    
    /* 摇摆的参数 */
    public float amount; // 摇摆幅度
    public float smoothAmount; // 平滑值
    public float maxAmount; // 最大摇摆幅度
    private Vector3 originalPosition; // 初始位置

    void Start()
    {
    
    
        // 自身位置(相对于父级物体变换得位置)
        originalPosition = transform.localPosition;
    }

    void Update()
    {
    
    
        // 设置武器手臂模型位置的值,(鼠标反转)
        float movementX = -Input.GetAxis("Mouse X") * amount;
        float movementY = -Input.GetAxis("Mouse Y") * amount;

        // 限制摇摆范围
        movementX = Mathf.Clamp(movementX, -maxAmount, maxAmount);
        movementY = Mathf.Clamp(movementY, -maxAmount, maxAmount);
        Vector3 finalPosition = new Vector3(movementX, movementY, 0);

        // 手柄位置变换
        transform.localPosition = Vector3.Lerp(transform.localPosition, finalPosition + originalPosition, Time.deltaTime * smoothAmount);
    }
}

Configuration parameters, you can configure it to your favorite feeling
Insert image description here
Effect, you can see that the gun will swing slightly when the angle of view is moved, which is very smart
Insert image description here

Source code

Source code is in the last section

end

Giving roses to others will leave a lingering fragrance in your hands! If the content of the article is helpful to you, please don't be stingy with your 点赞评论和关注 so that I can receive feedback as soon as possible. Every time you 支持 The greatest motivation for creation. Of course, if you find 存在错误 or 更好的解决方法 in the article, you are welcome to comment and send me a private message!

Good, I am向宇,https://xiangyu.blog.csdn.net

A developer who has been working quietly in a small company recently started studying Unity by himself out of interest. If you encounter any problems, you are also welcome to comment and send me a private message. Although I may not necessarily know some of the questions, I will check the information from all parties and try to give the best suggestions. I hope to help more people who want to learn programming. People, encourage each other~
Insert image description here

Guess you like

Origin blog.csdn.net/qq_36303853/article/details/134850351