Skip to content

Instantly share code, notes, and snippets.

View unitycoder's full-sized avatar
‏‏‎

mika unitycoder

‏‏‎
View GitHub Profile
@unitycoder
unitycoder / latlon.js
Created January 5, 2016 10:00
Calculate distance between LAT LON values
// calculate distance between two LAT LON values
// Note: In this formula, the latitudes and longitudes are expressed in radians. 6371 is radius of the earth in km
// SOURCE: codinggame.com
var earthRadius = 6371;
var x = (lonB-lonA)*Math.cos((latA+latB)/2);
var y = (latB-latA);
var d = Math.sqrt(x*x+y*y)*earthRadius;
@unitycoder
unitycoder / GetLayerNumber.cs
Last active November 13, 2023 19:42
Get Layernumber from Layermask
public LayerMask yourLayer = 1 << 10; // 10
gameObject.layer = (int)Mathf.Log(yourLayer.value, 2);
@unitycoder
unitycoder / UI2World.cs
Last active July 17, 2024 18:55
Move UI element to world gameobject position
// https://forum.unity.com/threads/overlay-canvas-and-world-space-coordinates.291108/#post-9627593
// translating between overlay canvas and world space coordinates
uiObject.transform.position = RectTransformUtility.WorldToScreenPoint(Camera.main, worldObject.transform.TransformPoint(Vector3.zero));
--------
// http://answers.unity3d.com/questions/799616/unity-46-beta-19-how-to-convert-from-world-space-t.html
//this is your object that you want to have the UI element hovering over
@unitycoder
unitycoder / UIOverlap.cs
Created February 18, 2016 13:33
Check if 2 UI Elements are overlapping (using Rect.Overlaps())
Vector3[] corners = new Vector3[4];
rectTransform1.GetWorldCorners(corners);
Rect rec = new Rect(corners[0].x,corners[0].y,corners[2].x-corners[0].x,corners[2].y-corners[0].y);
rectTransform2.GetWorldCorners(corners);
Rect rec2 = new Rect(corners[0].x, corners[0].y,corners[2].x-corners[0].x,corners[2].y-corners[0].y);
if (rectTransform1.Overlaps(rectTransform2))
{
Debug.Log("rectTransform1 is Overlapping rectTransform2!");
@unitycoder
unitycoder / UIScreenPos.cs
Created February 20, 2016 15:13
Get UI ScreenPosition & WorldPosition in separate Camera & Canvas
// Scene setup:
// CanvasCamera (canvasCam) with ScreenSpace-Camera UI canvas & elements (in different position from main camera)
// MainCamera (mainCam)
// UI image (obj) inside the CanvasCamera canvas
var originalScreenPos = canvasCam.WorldToScreenPoint(obj.position);
var worldPosInMainCamera = mainCam.ScreenToWorldPoint(p1);
var rayDirection = (worldPosInMainCamera-mainCam.transform.position);
Debug.DrawRay(mainCam.transform.position, rayDirection * 999, Color.red);
@unitycoder
unitycoder / e
Created February 21, 2016 14:57
3
X55b4f2f6990d5ca7a8X
@unitycoder
unitycoder / DrawRT.cs
Created February 22, 2016 14:23
Draw RenderTexture to Camera with Graphics.DrawTexture
using UnityEngine;
using System.Collections;
// attach this scrip to the camera you want to draw your rt from elsewhere
public class DrawRT : MonoBehaviour
{
public RenderTexture rt; // render to this rt from another camera
Camera cam;
Rect cameraViewRect;
http://answers.unity3d.com/questions/997174/unity-ui-rendertexture-broken-since-501p4.html *lower camera depth for rendertexture cam
http://forum.unity3d.com/threads/ui-flicker-screen-space-overlay-render-texture.375712/#post-2448703 *render camera manually in code
http://answers.unity3d.com/questions/1092350/unity-5-gui-fllickering-artefacts-on-android.html *fixed in unity version 5.3?
http://forum.unity3d.com/threads/5-3-elements-in-the-ui-flicker-between-different-sprites.373098/page-4#post-2486347 *patch version
@unitycoder
unitycoder / LatLong.cs
Last active April 17, 2024 16:06
Latitude Longitude Position on 3D Sphere
// blog post: https://unitycoder.com/blog/2016/03/01/latitude-longitude-position-on-3d-sphere-v2/
using UnityEngine;
public class LatLong : MonoBehaviour
{
public Transform marker; // marker object
public float radius = 5; // globe ball radius (unity units)
public float latitude = 51.5072f; // lat
@unitycoder
unitycoder / ClockwiseComparer.cs
Created March 8, 2016 15:46
Sort List of Vector2's to Clockwise order
// http://forum.unity3d.com/threads/create-2d-collider-based-on-non-transparent-pixels.390051/#post-2544449
/// <summary>
/// ClockwiseComparer provides functionality for sorting a collection of Vector2s such
/// that they are ordered clockwise about a given origin.
/// </summary>
public class ClockwiseComparer : IComparer<Vector2>
{
private Vector2 m_Origin;
/// <summary>