Project training (thirteen) - FPS game shooting feedback vibration screen and the number of UI bullets

I. Introduction

The content recorded in this blog is a continuation of the previous one. It will introduce the completed work such as the shooting feedback shake screen and the number of UI bullets. What is achieved here is the effect of shaking the screen after shooting and the number of bullets displayed on the UI of the game interface.
So far, the stand-alone development of the FPS game has been roughly completed, and then we will continue to multiplayer the game based on PUN2.

2. Shooting feedback shock screen

First created a CameraSpringUtility script. Values ​​is a strength exerted by spring.
What UpdateSpring achieves is the effect of the spring popping out and then gradually pulling the lens back.
The target is generally defined as the center of the circle. Without inheriting MonoBehaviour, there is no way to directly assign values ​​to components.

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

public class CameraSpringUtility
{
    
    
    public Vector3 Values;


    private float frequence;//过渡的效果
    private float damp;
    private Vector3 dampVaules;


    public CameraSpringUtility(float _frequence,float _damp)
    {
    
    
        frequence = _frequence;
        damp = _damp;
    }
    
    public void UpdateSpring(float _deltaTime, Vector3 _target)
    {
    
    
        Values -= _deltaTime * frequence * dampVaules;//从外面施加过来一个向上抬的力
        dampVaules = Vector3.Lerp(dampVaules, Values - _target, damp * _deltaTime);//衰减到target数值
    }
}

Then create a CameraSpring script that inherits MonoBehaviour so that it can be mounted on our object.

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;

public class CameraSpring : MonoBehaviour
{
    
    
    public float Frequence = 25;
    public float Damp = 15;


    public Vector2 MinRecoilRange;//输入的力的最小范围
    public Vector2 MaxRecoilRange;


    private CameraSpringUtility cameraSpringUtility;
    private Transform cameraSpringTransform;

    private void Start()
    {
    
    
        cameraSpringUtility = new CameraSpringUtility(Frequence, Damp);//新建一个对象
        cameraSpringTransform = transform;
    }


    private void Update()
    {
    
    
        cameraSpringUtility.UpdateSpring(Time.deltaTime, Vector3.zero);
        cameraSpringTransform.localRotation = Quaternion.Slerp(cameraSpringTransform.localRotation,
            Quaternion.Euler(cameraSpringUtility.Values), Time.deltaTime * 10);//
    }

	//施加力的一个方法
    public void StartCameraSpring()
    {
    
    
        cameraSpringUtility.Values = new Vector3(0,
            UnityEngine.Random.Range(MinRecoilRange.x, MaxRecoilRange.x),
            UnityEngine.Random.Range(MinRecoilRange.y, MaxRecoilRange.y));
    }
}

Then go to the FPMouseLook script, declare cameraSpring in the start function, find FiringForTest, and reference StartCameraSpring in it.

cameraSpring = GetComponentInChildren<CameraSpring>();
    public void FiringForTest()
    {
    
    
        currentRecoil += RecoilRange;
        cameraSpring.StartCameraSpring();
        currentRecoilTime = 0;
    }

Then go to the unity interface
insert image description here
insert image description here
and start the game, shoot, and you will find that a certain screen shaking effect has been achieved. The larger the frequency, the faster the jitter, and the larger the damp value, the faster the return to the center.

3. The number of UI bullets

After the code modification is completed and the UI is modified and added in unity, the number of bullets will be displayed in the lower right corner of the game interface. The number in front is the number of bullets remaining in the magazine, and the number in the back is the maximum number of bullets the magazine can hold.
insert image description here

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/Fancy145/article/details/125055928