0

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!

papi
  • 379
  • 7
  • 25
  • 1
    Everything that goes into the DLL can be hacked. You don't win anything by this, just a little delay before someone breaks it – Camilo Terevinto Jan 30 '18 at 00:11
  • 1
    That said, I believe your code should be in `FixedUpdate` and you should definitively use `DateTime.UtcNow` instead of `DateTime.Now` – Camilo Terevinto Jan 30 '18 at 00:14
  • @CamiloTerevinto yeah, it's true. But some people won't bother looking into DLL's, so precautions like this still matter. – papi Jan 30 '18 at 00:18
  • 2
    You should use `Stopwatch` for precision timing and not `DateTime.UtcNow` which is not as accurate. `Stopwatch` uses the high-resolution timer present on your computer, whereas `DateTime` uses the system (motherboard) real-time clock: https://stackoverflow.com/questions/2923283/stopwatch-vs-using-system-datetime-now-for-timing-events – Dai Jan 30 '18 at 01:13

0 Answers0