扣血抖动和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<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;
    }
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gxy_w

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值