I wrote this simple anti speed hack for unity, and in theory and practice it does (somewhat) works. But if the game is being alt+tabbed or minimized, or even sometimes out of nowhere, code thinks time has changed. I'm assuming it has something to do with the time lag? Anyways, here's the script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class AntiHack : MonoBehaviour {
public static float systemTime = 0F;
public static float timer = 0F;
public static bool kickPlayer = false;
public float tolerance = 1.1f;
int resetTime = 0;
// UI Canvas
public Canvas antihack_Canvas;
void Start()
{
systemTime = System.DateTime.Now.Second;
timer = System.DateTime.Now.Second;
}
void Update()
{
if (systemTime == 10F || systemTime == 50F)
{
timer = System.DateTime.Now.Second;
resetTime = 0;
}
systemTime = System.DateTime.Now.Second;
timer += Time.deltaTime;
float result = timer - systemTime;
if (result < 0 )
{
result = result * -1;
}
if (result > tolerance && kickPlayer == true && systemTime > 10F)
{
KickPlayer();
}
if (result > 60 + tolerance && systemTime < 10F && kickPlayer == true)
{
KickPlayer();
}
}
//If player got caught do this
void KickPlayer()
{
Application.LoadLevel ("antiHackScreen");
}
public static void RestartTimer(bool kick)
{
timer = System.DateTime.Now.Second;
kickPlayer = kick;
}
}
And I'm using this when the level is being started:
public class restartTimer : MonoBehaviour {
// Use this for initialization
void Start () {
AntiHack.RestartTimer (true);
}
}
Any help would be appreciated! Thank you!