一、扣血抖动
打斗的时候,当受到伤害时,为了使游戏更加真实,会使主角抖动;但这只是让玩家看起来的,其实只需要抖动窗口即可实现。 其原理是摄像机窗口,既屏幕视口。
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;
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;
}
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();
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;
}
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);
}
}
|