Last active
October 24, 2025 13:33
-
-
Save Bradshaw/03b483d984b110def85ca778e5080793 to your computer and use it in GitHub Desktop.
Unity code snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class DestroyAfterSeconds : MonoBehaviour | |
| { | |
| public float seconds; | |
| // Start is called before the first frame update | |
| void Start() | |
| { | |
| Destroy(gameObject, seconds); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class DestroyOffscreen : MonoBehaviour | |
| { | |
| public float margin; | |
| // Update is called once per frame | |
| void Update() | |
| { | |
| Vector3 screenPoint = Camera.main.WorldToScreenPoint(transform.position); | |
| if (screenPoint.x < -margin || screenPoint.x > Screen.width + margin || screenPoint.y < -margin || screenPoint.y > Screen.height + margin) | |
| { | |
| Destroy(gameObject); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class JoystickMove : MonoBehaviour | |
| { | |
| Collider2D myCollider; | |
| public ContactFilter2D collisionMask; | |
| public float radius; | |
| Vector2 velocity; | |
| Vector2 jerk; | |
| public float speed = 5; | |
| public float smoothTime = 0.5f; | |
| // Start is called before the first frame update | |
| void Start() | |
| { | |
| velocity = Vector2.zero; | |
| myCollider = GetComponent<Collider2D>(); | |
| } | |
| Vector2 GetInput() | |
| { | |
| Vector2 input = Input.GetAxis("Horizontal") * Vector2.right + Input.GetAxis("Vertical") * Vector2.up; | |
| if (input.sqrMagnitude > 1) | |
| { | |
| input = input.normalized; | |
| } | |
| return input; | |
| } | |
| // Update is called once per frame | |
| void Update() | |
| { | |
| velocity = Vector2.SmoothDamp(velocity, GetInput(), ref jerk, smoothTime); | |
| Vector3 movement = velocity.XY_() * speed * Time.deltaTime; | |
| // Avec Collider | |
| Vector3 beforeMove = transform.position; | |
| transform.position += movement; | |
| if (Physics2D.OverlapCollider(myCollider, collisionMask, new List<Collider2D>()) > 0) | |
| { | |
| transform.position = beforeMove; | |
| } | |
| // Avec CircleCast | |
| // RaycastHit2D hit = Physics2D.CircleCast(transform.position, radius, movement); | |
| // if (hit) | |
| // { | |
| // transform.position = hit.centroid; | |
| // } | |
| // else | |
| // { | |
| // transform.position += movement; | |
| // } | |
| List<RaycastHit2D> hits = new List<RaycastHit2D>(); | |
| int count = Physics2D.CircleCast(transform.position, radius, movement.normalized, collisionMask, hits, movement.magnitude); | |
| if (count > 0) | |
| { | |
| Vector3 closest = transform.position + movement; | |
| float distance = movement.magnitude; | |
| foreach (RaycastHit2D hit in hits) | |
| { | |
| if (hit.distance < distance) | |
| { | |
| closest = hit.centroid; | |
| distance = hit.distance; | |
| } | |
| } | |
| transform.position = closest; | |
| } | |
| else | |
| { | |
| transform.position += movement; | |
| } | |
| transform.rotation = Quaternion.identity; | |
| transform.localRotation = Quaternion.identity; | |
| } | |
| void OnDrawGizmosSelected() | |
| { | |
| if (Application.isPlaying) | |
| { | |
| Color oldColor = Gizmos.color; | |
| Gizmos.color = Color.blue; | |
| Gizmos.DrawRay(transform.position, GetInput().XY_()); | |
| Gizmos.color = Color.red; | |
| Gizmos.DrawRay(transform.position, velocity.XY_()); | |
| Gizmos.color = oldColor; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class QuitOnEscape : MonoBehaviour | |
| { | |
| // Update is called once per frame | |
| void Update() | |
| { | |
| if (Input.GetKey(KeyCode.Escape)) | |
| { | |
| Application.Quit(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class SmoothFollow : MonoBehaviour | |
| { | |
| public Transform target; | |
| public float smoothTime; | |
| Vector3 offset; | |
| Vector3 velocity; | |
| // Start is called before the first frame update | |
| void Start() | |
| { | |
| offset = transform.position - target.position; | |
| } | |
| // Update is called once per frame | |
| void Update() | |
| { | |
| transform.position = Vector3.SmoothDamp( | |
| transform.position, | |
| target.position + offset, | |
| ref velocity, | |
| smoothTime | |
| ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class StayOnScreen : MonoBehaviour | |
| { | |
| public float margin; | |
| public bool wrap = false; | |
| // Update is called once per frame | |
| void LateUpdate() | |
| { | |
| Vector3 screenPoint = Camera.main.WorldToScreenPoint(transform.position); | |
| float screenMargin = Vector3.Distance( | |
| screenPoint, | |
| Camera.main.WorldToScreenPoint(transform.position + Vector3.right * margin) | |
| ); | |
| if (wrap) | |
| { | |
| screenPoint = new Vector3( | |
| Wrap(screenPoint.x, -screenMargin, Screen.width + screenMargin), | |
| Wrap(screenPoint.y, -screenMargin, Screen.height + screenMargin), | |
| screenPoint.z | |
| ); | |
| } | |
| else | |
| { | |
| screenPoint = new Vector3( | |
| Mathf.Clamp(screenPoint.x, screenMargin, Screen.width - screenMargin), | |
| Mathf.Clamp(screenPoint.y, screenMargin, Screen.height - screenMargin), | |
| screenPoint.z | |
| ); | |
| } | |
| transform.position = Camera.main.ScreenToWorldPoint(screenPoint); | |
| } | |
| float Wrap(float a, float min, float max) | |
| { | |
| float gap = max - min; | |
| return min + Mathf.Repeat(a - min, gap); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using UnityEngine.SceneManagement; | |
| public static class Utils | |
| { | |
| public static void NotifyAll(string methodName) | |
| { | |
| foreach (GameObject go in SceneManager.GetActiveScene().GetRootGameObjects()) | |
| { | |
| go.BroadcastMessage(methodName, SendMessageOptions.DontRequireReceiver); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment