Skip to content

Instantly share code, notes, and snippets.

@omikun
omikun / vectormath.cs
Last active November 16, 2016 00:47
Vector math
float AngleBetweenVector2(Vector2 vec1, Vector2 vec2)
{
Vector2 diference = vec2 - vec1;
float sign = (vec2.y < vec1.y)? -1.0f : 1.0f;
return Vector2.Angle(Vector2.right, diference) * sign;
}
float cosAngleBetweenVector3(Vector3 vec1, Vector3, vec2)
{
float dot = Vector3.Dot(vec1,vec2);
@omikun
omikun / limit_max_memory_exec.sh
Created December 7, 2016 19:46
limits the maximum memory used by process (ls in example) to 10MB
# Create a memory cgroup named 'mygroup', allowing $USER to change the parameters and run tasks in the cgroup.
sudo cgcreate -a $USER -t $USER -g memory:mygroup
# Set the memory limit to 10MB
echo 10000000 > /sys/fs/cgroup/memory/mygroup/memory.limit_in_bytes
# Run a program in the cgroup
cgexec -g memory:mygroup ls
@omikun
omikun / Project3dVectorOnToPlane.cs
Created July 10, 2017 20:41
Project a 3d vector onto a plane, convert 3D to 2D vector, useful for rendering a 2D indicator over 3D element or get direction of 3D element if out of frame
//taken from http://answers.unity3d.com/questions/1097270/project-a-vector3-onto-a-plane-orthographically.html
//You simply project your vector onto the normal and subtract the result from your original vector. That will give you the projected vector on the plane. If you want to project a point to a plane you first need to calculate the relative vector from a point on that plane.
Vector3 planeOrigin;
Vector3 planeNormal;
Vector3 point;
Vector3 v = point - planeOrigin;
Vector3 d = Vector3.Project(v, planeNormal.normalized);
Vector3 projectedPoint = point - d;
@omikun
omikun / CameraMovement.cs
Created July 11, 2017 19:04
Follows target like in flight sim
//taken from https://github.com/razvanilin/Nebulae/blob/master/Assets/Scripts/CameraMovement.cs
using UnityEngine;
using System.Collections;
public class CameraMovement : MonoBehaviour {
public Transform target;
public float distance = 2.0f;
public float height;
@omikun
omikun / DogCurve.cs
Last active July 18, 2017 02:23
Missile homing algorithm
//now supports missile acceleration!
Vector3 SlowDogCurve()
{
min.Clear();
var line = GetComponent<LineRenderer>();
//find desired velocity vector
var Vt = target.GetComponent<Rigidbody>().velocity;
var Vm0 = rb.velocity;
var Pt0 = target.transform.position;
var Pm0 = transform.position;
@omikun
omikun / preventJitter.cs
Created July 12, 2017 23:45
How to stop jittering objects
//http://answers.unity3d.com/questions/353132/smoothdamp-causes-jitters.html
body.interpolation = RigidbodyInterpolation.Interpolate;
don't parent target to camera that follows it, especially if target or camera is physics driven
if target is physics drive, use fixedUpdate on camera
@omikun
omikun / tipsandtricks.cs
Created July 13, 2017 17:27
Tips and tricks when working in Unity
[ContextMenu("function")]
private void myfunction()
{
//function can be called by right clicking on component and select function
}
@omikun
omikun / Timer.cs
Last active July 27, 2017 00:46
helper classes for managing times w/o an event scheduler
public class Timer {
public float StartTime;
public float Duration;
bool Idle;
public Timer(float d)
{
Idle = false;
Duration = d;
StartTime = Time.time;
}
@omikun
omikun / scythe_board.cs
Last active November 10, 2017 01:50
Scythe hex board init code
using System.IO;
using System;
//data structure for hex grid
class HexLink
{
public Hex link = null;
public bool river = false;
public HexLink(Hex h=null, bool r=false)
{
@omikun
omikun / game_definition.txt
Created November 10, 2017 01:50
definition of scythe game rules, draft1
//define game
win condition: player with most coins
player has stats {powers, coins, hearts, mechs, buildings, enlistments}
player has x cards, each card with 2 actions {top row action, bottom row action}
player move in hex grid
player chooses one action card and perform up to two actions
nouns: tile, resource, mech, building, coin, power, heart, enlistment
verbs: move, build, deploy, upgrade, enlist,