{"id":16615527,"url":"https://github.com/domiii/unitypractice","last_synced_at":"2026-06-08T07:33:43.640Z","repository":{"id":137713501,"uuid":"79459153","full_name":"Domiii/UnityPractice","owner":"Domiii","description":"Random collection of Game Code Snippets","archived":false,"fork":false,"pushed_at":"2017-12-25T05:30:16.000Z","size":3514,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-12-19T15:56:02.687Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Domiii.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-19T14:05:16.000Z","updated_at":"2018-08-11T19:24:07.000Z","dependencies_parsed_at":"2023-03-27T15:49:54.649Z","dependency_job_id":null,"html_url":"https://github.com/Domiii/UnityPractice","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Domiii/UnityPractice","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Domiii%2FUnityPractice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Domiii%2FUnityPractice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Domiii%2FUnityPractice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Domiii%2FUnityPractice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Domiii","download_url":"https://codeload.github.com/Domiii/UnityPractice/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Domiii%2FUnityPractice/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34053435,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-08T02:00:07.615Z","response_time":111,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-12T02:09:42.518Z","updated_at":"2026-06-08T07:33:43.622Z","avatar_url":"https://github.com/Domiii.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# UnityPractice\n\n## Random Snippet TODO list\n\n### Basic Stuff\n\n* Scene refresh + Scene change\n* GameManager -\u003e Score counting\n* Playing with physical buttons\n\n### UI\n\n* Light switch\n* Quiz UI\n\n\n### Colliders, Meshes + Materials\n\n* Getting + working with a Collider programmatically\n* Working with bounding box\n* Getting + working with a Mesh programmatically\n* Modifying a mesh\n* Programmatic terrain\n\n\n### Bigger Experiments + Tutorials\n\n* How to do a rule-based AI?\n\n\n\n### Some missing scripts\n\n// TODO: ObjectTracker + Arrows\n//  -\u003e https://github.com/Domiii/UnityBoxes/tree/master/Assets/Scripts/Tracking\n\n// TODO: LevelManager\n\n// PlaySoundOnClick.cs\n\nusing UnityEngine;\nusing System.Collections;\n\npublic class PlaySoundOnClick : MonoBehaviour {\n    void OnMouseDown() {\n        var audioSource = GetComponent\u003cAudioSource\u003e();\n        if (audioSource != null) {\n            audioSource.Play();\n        }\n    }\n}\n\n// ####################################\n// MoveToDestination.cs\n// ####################################\n\nusing UnityEngine;\nusing System.Collections;\n \npublic class MoveToDestinationAction : AIAction {\n  public Vector3 destination;\n}\n\n/// \u003csummary\u003e\n/// Move to destination, don't let anything distract you from that.\n/// \u003c/summary\u003e\n[RequireComponent(typeof(NavMeshMover))]\npublic class MoveToDestination : AIStrategy\u003cMoveToDestinationAction\u003e {\n  NavMeshMover mover;\n\n  void Awake () {\n    mover = GetComponent\u003cNavMeshMover\u003e ();\n  }\n\n  #region Public\n  public Vector3 CurrentDestination {\n    get { \n      return mover.CurrentDestination;\n    }\n  }\n\n  public override void StartBehavior(MoveToDestinationAction action) {\n    mover.CurrentDestination = action.destination;\n    mover.StopMovingAtDestination = true;\n  }\n  #endregion\n\n  void Update () {\n    if (mover.HasArrived) {\n      StopStrategy ();\n    }\n  }\n\n  /// \u003csummary\u003e\n  /// Called when finished moving.\n  /// \u003c/summary\u003e\n  protected override void OnStop() {\n    mover.StopMove ();\n  }\n}\n\n// ####################################\n// More\n// ####################################\n\n  {\n    name: 'MoveToClickPoint',\n    tags: ['navmesh'],\n    code:\n`NavMeshAgent agent;\n\nvoid Start() {\n   agent = GetComponent\u003cNavMeshAgent\u003e();\n}\n\nvoid Update() {\n    if (Input.GetMouseButtonDown(0)) {\n       RaycastHit hit;\n       if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100)) {\n            agent.destination = hit.point;\n        }\n    }\n}`\n  },\n  {\n    name: 'WallSpawner',\n    code:\n`public int Nx = 10;\npublic int Ny = 10;\npublic float CubeSize = 0.5f;\npublic Material[] Materials = new Material[0];\nprivate float Gap = 0.01f;\n\nvoid Start () {\n  if (transform.childCount == 0) {\n    CreateBricks ();\n  }\n}\n\npublic void CreateBricks() {\n  for (var j = 0; j \u003c Ny; ++j) {\n    var m = j % Materials.Length;\n    for (var i = 0; i \u003c Nx; ++i) {\n      var cube = GameObject.CreatePrimitive (PrimitiveType.Cube);\n      cube.transform.SetParent(transform, false);\n      cube.transform.localScale = new Vector3(CubeSize, CubeSize, CubeSize);\n\n      var x = i * (CubeSize + Gap) + Gap;\n      var y = j * (CubeSize + Gap) + Gap;\n\n      cube.transform.localPosition = new Vector3 (x, y, 0);\n      cube.AddComponent\u003cRigidbody\u003e();\n      cube.AddComponent\u003cBoxCollider\u003e();\n\n      cube.GetComponent\u003cMeshRenderer\u003e ().material = Materials[m];\n      m = (m + 1) % Materials.Length;\n    }\n  }\n}`\n  },\n  {\n    name: 'Sun',\n    title_en: '',\n    note: 'Increase Y value of AxisOfRotation to reduce the angle, increase X to increase it.',\n    code:\n`/// \u003csummary\u003e\n/// Virtual time of day from 0 = 00:00 to 1 = 00:00 on the next day\n/// \u003c/summary\u003e\npublic float VirtualTimeOfDay;\npublic float RealSecondsPerDay = 10;\npublic Vector3 AxisOfRotation = new Vector3 (2, 1, 0);\n\nvoid Start ()\n{\n  // get current time of day\n  var systemTimeOfDay = System.DateTime.Now.TimeOfDay;\n  VirtualTimeOfDay = (float)systemTimeOfDay.TotalDays;\n\n  AxisOfRotation.Normalize (); // normalize axis\n}\n\nvoid Update ()\n{\n  // advance time of day\n  var timeDiff = Time.deltaTime;\n  var virtualTimeDiff = timeDiff / RealSecondsPerDay;\n  VirtualTimeOfDay += virtualTimeDiff;\n  VirtualTimeOfDay = Mathf.Repeat (VirtualTimeOfDay, 1);\n\n  // update rotation\n  transform.localRotation = Quaternion.AngleAxis (360 * VirtualTimeOfDay, AxisOfRotation);\n}`\n  },\n  {\n    name: 'HaloPickup',\n    title_en: 'Hovers over player\\'s head when picked up.',\n    code:\n`public Player player;\npublic Vector3 relativePosition = new Vector3(0, 1, 0);\npublic Vector3 bobbingRadius = new Vector3(1, 0.4f, 1);\npublic float bobbingSpeed = 0.5f;\n\nVector2 startPos;\n\npublic bool IsEquipped {\n  get {\n    return player != null;\n  }\n}\n\nvoid Start() {\n  startPos = transform.position;\n}\n\nvoid Update() {\n  if (IsEquipped) {\n    Hover ();\n\n    if (Input.GetKeyDown (KeyCode.Space)) {\n      Unequip ();\n    }\n  }\n}\n\nvoid Hover() {\n  var pos = player.transform.position + relativePosition;\n  var theta = Time.time * 2 * Mathf.PI * bobbingSpeed;\n  pos.x += Mathf.Sin(theta) * bobbingRadius.x;\n  pos.z += Mathf.Cos(theta) * bobbingRadius.z;\n  pos.y += Mathf.Sin(0.5f * theta) * bobbingRadius.y;\n  transform.position = pos;\n}\n\nvoid OnTriggerEnter(Collider other) {\n  if (IsEquipped) {\n    // don't do anything when already equipped\n    return;\n  }\n\n  var triggerPlayer = other.GetComponentInParent\u003cPlayer\u003e ();\n  if (triggerPlayer != null) {\n    Equip (triggerPlayer);\n  }\n}\n\nvoid Equip(Player triggerPlayer) {\n  player = triggerPlayer;\n  transform.localScale /= 2;\n}\n\n/// \u003csummary\u003e\n/// Player dropped (放下) the halo\n/// \u003c/summary\u003e\nvoid Unequip() {\n  transform.localScale *= 2;\n  player = null;\n  transform.position = startPos;\n}`\n  },\n  {\n    name: 'RollingCube',\n    title_en: 'Let a 1x1x1 sized cube roll!',\n    code:\n`public float speed = 2;\n\npublic Transform left, right, forward, back, allRotators;\n\n\nQuaternion rollRotation = Quaternion.Euler(new Vector3 (0, 0, 90));\nfloat progress;\nQuaternion startRotatorRotation, endRotatorRotation;\n\npublic bool IsRolling {\n  get { return transform.parent != null; }\n}\n\nvoid Reset () {\n  // create all rotators (make sure, their x-axis is pointing toward the center of the cube)\n  allRotators = new GameObject (\"AllRotators\").transform;\n  allRotators.position = transform.position;\n\n  left = CreateRotator (\"Left\", Vector3.left * 0.5f, Quaternion.identity);\n  right = CreateRotator (\"Right\", Vector3.right * 0.5f, Quaternion.Euler(new Vector3(0, 180, 0)));\n  back = CreateRotator (\"Back\", Vector3.back * 0.5f, Quaternion.Euler(new Vector3(0, 270, 0)));\n  forward = CreateRotator (\"Forward\", Vector3.forward * 0.5f, Quaternion.Euler(new Vector3(0, 90, 0)));\n}\n\nTransform CreateRotator(string name, Vector3 pos, Quaternion rotation) {\n  var go = new GameObject(name);\n  pos += Vector3.down * 0.5f;\n\n  var newTransform = go.transform;\n  newTransform.SetParent(transform);\n  newTransform.localPosition = pos;\n  newTransform.rotation = rotation;\n  newTransform.SetParent(allRotators);\n\n  return newTransform;\n}\n\nvoid FixedUpdate () {\n  if (!IsRolling) {\n    // not rolling -\u003e We can start rolling\n    var horizontal = Input.GetAxis (\"Horizontal\");\n    var vertical = Input.GetAxis (\"Vertical\");\n    if (horizontal \u003c 0) {\n      StartRoll (left);\n    }\n    else if (horizontal \u003e 0) {\n      StartRoll (right);\n    }\n    else if (vertical \u003c 0) {\n      StartRoll (back);\n    }\n    else if (vertical \u003e 0) {\n      StartRoll (forward);\n    }\n  } else {\n    // keep on rolling!\n    UpdateRoll();\n  }\n}\n\n\nvoid StartRoll(Transform rotator) {\n  // add cube to rotator\n  transform.SetParent (rotator);\n\n  // set start and end rotation\n  startRotatorRotation = rotator.rotation;\n  endRotatorRotation = rotator.rotation * rollRotation;\n}\n\n\nvoid UpdateRoll() {\n  // update progress (let progress go from 0 to 1 in 1/speed seconds)\n  progress = Mathf.Min(1, progress + speed * Time.deltaTime);\n\n  // interpolate rotation between start and end rotations\n  var progressTransform = transform.parent.transform;\n  progressTransform.rotation = Quaternion.Lerp(startRotatorRotation, endRotatorRotation, progress);\n\n  if (progress \u003e= 1) {\n    // finished rolling\n    EndRoll();\n  }\n}\n\n\nvoid EndRoll() {\n  // move all rotators\n  allRotators.position = transform.position;\n\n  // reset progress \u0026 rotator's rotation\n  var rotator = transform.parent;\n  rotator.rotation = startRotatorRotation;\n  progress = 0;\n\n\n  // remove from parent\n  transform.SetParent (null);\n}`\n  },\n  {\n    name: 'Elevator',\n    title_en: '',\n    note: 'Quite advanced!',\n    code: \n`/// \u003csummary\u003e\n/// The distance between two floors\n/// \u003c/summary\u003e\npublic Vector3 FloorDistance = Vector3.up;\npublic float Speed = 1.0f;\npublic int Floor = 0;\npublic int MaxFloor = 1;\npublic Transform moveTransform;\n\nprivate float tTotal;\nprivate bool isMoving;\nprivate float moveDirection;\n\n\n// Use this for initialization\nvoid Start () {\n  moveTransform = moveTransform ?? transform;\n}\n\n// Update is called once per frame\nvoid Update () {\n  if (isMoving) {\n    // elevator is moving\n    MoveElevator();\n  }\n}\n\nvoid MoveElevator() {\n  var v = moveDirection * FloorDistance.normalized * Speed;\n  var t = Time.deltaTime;\n  var tMax = FloorDistance.magnitude / Speed;\n  t = Mathf.Min (t, tMax - tTotal);\n  moveTransform.Translate(v * t);\n  tTotal += t;\n  print (tTotal);\n\n  if (tTotal \u003e= tMax) {\n    // we arrived on floor\n    isMoving = false;\n    tTotal = 0;\n    Floor += (int)moveDirection;\n    print (string.Format(\"elevator arrived on floor {0}!\", Floor));\n  }\n}\n\n/// \u003csummary\u003e\n/// Start moving up one floor\n/// \u003c/summary\u003e\npublic void StartMoveUp() {\n  if (isMoving)\n    return;\n\n  isMoving = true;\n  moveDirection = 1;\n}\n\n/// \u003csummary\u003e\n/// Start moving down one floor\n/// \u003c/summary\u003e\npublic void StartMoveDown() {\n  if (isMoving)\n    return;\n\n  isMoving = true;\n  moveDirection = -1;\n}\n\n/// \u003csummary\u003e\n/// Tell the elevator to move up or down\n/// \u003c/summary\u003e\npublic void CallElevator() {\n  if (isMoving)\n    return;\n\n  print (\"elevator starts moving!\");\n\n  // start moving\n  if (Floor \u003c MaxFloor) {\n    StartMoveUp ();\n  }\n  else {\n    StartMoveDown ();\n  }\n\n}`\n  },\n  {\n    name: 'ElevatorButton',\n    title_en: 'A Button for the Elevator!',\n    code: \n`public Elevator Elevator;\n\nvoid OnTriggerEnter(Collider collider) {\n  var go = collider.gameObject;\n  var player = go.GetComponent\u003cPlayer\u003e ();\n\n  if (player != null \u0026\u0026 Elevator != null) {\n    Elevator.CallElevator ();\n  }\n}`\n  },\n  {\n    name: 'MoveWithTarget',\n    title_en: '',\n    code: \n``\n  },\n  {\n    name: 'ColorOnCollision',\n    title_en: '',\n    code: \n`Renderer ownRenderer;\n\nvoid Start() {\n  ownRenderer = GetComponent\u003cRenderer\u003e ();\n}\n\nvoid OnCollisionEnter(Collision other) {\n  if (other.gameObject.GetComponent\u003cPlayer\u003e() != null) {\n    // color when colliding with player!\n    var otherMaterial = other.gameObject.GetComponent\u003cRenderer\u003e ().material;\n    ownRenderer.material.Lerp (ownRenderer.material, otherMaterial, 0.5f);\n  }\n}`\n  },\n  {\n    name: '',\n  title_en: '',\n    code: \n``\n  },\n  {\n    name: '',\n  title_en: '',\n    code: \n``\n  },\n];\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdomiii%2Funitypractice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdomiii%2Funitypractice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdomiii%2Funitypractice/lists"}