栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

扣血抖动和FPS显示

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

扣血抖动和FPS显示

一、扣血抖动

打斗的时候,当受到伤害时,为了使游戏更加真实,会使主角抖动;但这只是让玩家看起来的,其实只需要抖动窗口即可实现。
其原理是摄像机窗口,既屏幕视口。

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();
        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;
    }
}

二、FPS的显示

玩射击游戏的时候,或者玩其他游戏,会有一个显示帧率的,可能是真的,也有可能的固定的。自己的一些小游戏可以实现。

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);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901818.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号