Skip to content

Instantly share code, notes, and snippets.

View Keldrik's full-sized avatar

Thomas Wiegold Keldrik

View GitHub Profile
@Keldrik
Keldrik / UnityMouseToWorldPoint.cs
Last active August 29, 2015 14:01
Unity: Convert Mouse Position to Point in World Space
public Vector3 MouseToWorldPoint(Camera cam, float distance)
{
var m = Input.mousePosition;
m.z = distance;
return cam.ScreenToWorldPoint(m);
}
@Keldrik
Keldrik / UnityCheckMouseHitObjectByTag.cs
Created May 21, 2014 15:03
Unity: Check if Mouse hits gameobject with specified tag
public bool CheckMouseHitObjectByTag(Camera cam, string tag)
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (tag == hit.collider.tag)
{
return true;
}
@Keldrik
Keldrik / UnityParticleAutoDestroy.cs
Created February 17, 2015 16:40
Unity Script: Destroy non-looping particle objects automatically when they are no longer alive
using UnityEngine;
using System.Collections;
public class ParticleAutoDestroy : MonoBehaviour
{
private ParticleSystem ps;
void Start()
{
ps = GetComponent<ParticleSystem>();
@Keldrik
Keldrik / useFetch.js
Last active November 12, 2018 03:25
React Hooks - Using useState and useEffect for a simple generic data fetch
import { useState, useEffect } from "react";
const useFetch = url => {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(async () => {
const response = await fetch(url);
const data = await response.json();
const [item] = data.results;
@Keldrik
Keldrik / hooks-fetch-beispiel1.jsx
Last active August 4, 2019 23:55
Fetch mit React Hooks - Beispiele
import React, { useState } from 'react';
const beispiel = () => {
const [count, setCount] = useState(0);
return (
<div>
<p>Du hast {count} mal geklickt!</p>
<button onClick={() => setCount(count + 1)}>
Klick mich!
</button>
import React, { useState, useEffect } from 'react';
const beispiel = () => {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `Du hast ${count} mal geklickt!`;
});
return (
import { useState, useEffect } from 'react';
const useDataFetch = url => {
const [data, setData] = useState(null);
useEffect(() => {
const loadData = async url => {
const response = await fetch(url);
const json = await response.json();
setData(json);
};
import React from 'react';
import useDataFetch from './useDataFetch';
const app = () => {
const data = useDataFetch('https://jsonplaceholder.typicode.com/posts/1');
const postTitle = () => {
if (data) {
return <h2>{data.title}</h2>;
else {
@Keldrik
Keldrik / randomStringLettersNumbers.js
Created August 16, 2019 13:07
Helper module for creating random strings with letters and numbers.
const randomString = (length) => {
return [...Array(length)].map(() => Math.random().toString(36)[2]).join('');
}
export default randomString;
// Setzt die Position des script ausführenden GameObjects
// auf x:1, y:0, z:5
this.transform.position.Set(1f, 0f, 5f);
// Hat den selben Effekt, nur weisen wir hier einen
// neuen Vector3 zu
this.transform.position = new Vector3(1f, 0f, 5f);