Advertisement
Hygcgggnngff

anti cheat by antoca

Aug 4th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Networking; // Use this if you're working with Unity's UNet or a similar networking solution
  3.  
  4. public class BasicAntiCheat : MonoBehaviour
  5. {
  6. public float maxAllowedSpeed = 10f; // Maximum legitimate speed
  7. public float maxTeleportDistance = 20f; // Maximum distance a player can move in a single frame without it being considered teleportation
  8.  
  9. private Vector3 lastPosition; // To track the player's position over time
  10. private float lastSpeedCheckTime; // To track time between speed checks
  11.  
  12. void Start()
  13. {
  14. // Initialize lastPosition with the player's starting position
  15. lastPosition = transform.position;
  16. lastSpeedCheckTime = Time.time;
  17. }
  18.  
  19. void Update()
  20. {
  21. // Call these checks each frame
  22. CheckForSpeedHacks();
  23. CheckForTeleportation();
  24. }
  25.  
  26. void CheckForSpeedHacks()
  27. {
  28. // Calculate the distance the player has traveled since the last check
  29. float distanceTraveled = Vector3.Distance(lastPosition, transform.position);
  30.  
  31. // Calculate the time elapsed since the last check
  32. float timeElapsed = Time.time - lastSpeedCheckTime;
  33.  
  34. // Calculate the player's current speed
  35. float currentSpeed = distanceTraveled / timeElapsed;
  36.  
  37. // If the player's speed exceeds the maxAllowedSpeed, trigger an anti-cheat response
  38. if (currentSpeed > maxAllowedSpeed)
  39. {
  40. Debug.LogWarning("Speed hack detected!");
  41. // Take action: e.g., log out, ban, or reset player position
  42. // HandleCheatDetected();
  43. }
  44.  
  45. // Update the last check time and position
  46. lastSpeedCheckTime = Time.time;
  47. lastPosition = transform.position;
  48. }
  49.  
  50. void CheckForTeleportation()
  51. {
  52. // Calculate the distance the player has moved since the last frame
  53. float distanceMoved = Vector3.Distance(lastPosition, transform.position);
  54.  
  55. // If the distance moved exceeds the maxTeleportDistance, trigger an anti-cheat response
  56. if (distanceMoved > maxTeleportDistance)
  57. {
  58. Debug.LogWarning("Teleportation hack detected!");
  59. // Take action: e.g., log out, ban, or reset player position
  60. // HandleCheatDetected();
  61. }
  62.  
  63. // Update last position
  64. lastPosition = transform.position;
  65. }
  66.  
  67. void HandleCheatDetected()
  68. {
  69. // Implement your response to cheating here
  70. // For example, log the player out, reset their position, ban them, etc.
  71. }
  72. }
  73.  
Tags: anti cheat
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement