Blood deduction jitter and FPS display

1. Blood deduction and shaking

When fighting, when taking damage, in order to make the game more realistic, the protagonist will shake; but this is just for the player to look, in fact, only need to shake the window to achieve.
Its principle is the camera window, which is the screen viewport.

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

public class ShakeCamera : MonoBehaviour
{
    
    

    public float ShakeLevel = 3f;//振动幅度
    public float setShakeTime = 0.5f;//振动时间
    public float shakeFps = 45f;//振动的fps

    public bool isShakeCamera = false;
    public float fps;
    public float shakeTime = 0.0f;
    public float frameTime = 0.0f;
    public float shakeDelta = 0.005f;
    public Camera mainCamera;

    void OnEnable()
    {
    
    
        isShakeCamera = true;
        mainCamera = this.GetComponent<Camera>();
        shakeTime = setShakeTime;
        fps = shakeFps;
        frameTime = 0.03f;
        shakeDelta = 0.005f;
    }

    // Update is called once per frame
    void Update()
    {
    
    
        if (isShakeCamera)
        {
    
    
            if (shakeTime > 0)
            {
    
    
                shakeTime -= Time.deltaTime;
                if (shakeTime <= 0)
                {
    
    
                    enabled = false;
                }
                else
                {
    
    
                    frameTime += Time.deltaTime;
                    if (frameTime > 1.0 / fps)
                    {
    
    
                        frameTime = 0;
                        mainCamera.rect = new Rect(shakeDelta * (-1.0f + ShakeLevel * Random.value), shakeDelta * (-1.0f + ShakeLevel * Random.value), 1.0f, 1.0f);
                    }
                }
            }
        }
    }
    void OnDisable()
    {
    
    
        mainCamera.rect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
        isShakeCamera = false;
    }
}

2. Display of FPS

When playing shooting games, or playing other games, there will be a display frame rate, which may be real, or it may be fixed. Some small games of your own can be realized.

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


/*
 * 帧率
 * 
 */
public class FPS : MonoBehaviour {
    
    

    private float lastUpdateShowTime = 0;//上一次更新帧率的时间
    private float updateTime = 0.05f;//更新显示帧率的时间间隔
    private int frames = 0;//帧数
    private float frameDeltaTime = 0;//帧间间隔

    private float Fps;
    private Rect fps, deltaTime;
    private GUIStyle style = new GUIStyle();
	// Use this for initialization
	void Awake () {
    
    
        Application.targetFrameRate = 100;
	}
	void Start()
    {
    
    
        lastUpdateShowTime = Time.realtimeSinceStartup;
        fps = new Rect(0, 0, 100, 100);
        deltaTime=new Rect(0,30,100,100);
        style.fontSize = 30;
        style.normal.textColor = Color.red;
    }
	// Update is called once per frame
	void Update () {
    
    
        frames++;
        if (Time.realtimeSinceStartup-lastUpdateShowTime>=updateTime)
        {
    
    
            Fps = frames / (Time.realtimeSinceStartup - lastUpdateShowTime);
            frameDeltaTime = (Time.realtimeSinceStartup - lastUpdateShowTime) / frames;
            frames = 0;
            lastUpdateShowTime = Time.realtimeSinceStartup;
        }
	}
    void OnGUI()
    {
    
    
        GUI.Label(fps, "FPS:" + Fps, style);
        GUI.Label(deltaTime, "时间间隔:" + frameDeltaTime, style);
    }
}

Guess you like

Origin blog.csdn.net/lml_w/article/details/123999691