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 / UnityBewegungMitPfeiltasten.cs
Last active March 9, 2023 22:55
Unity Einfache Bewegung Beispiel Script
using UnityEngine;
using System.Collections;
public class Bewegen : MonoBehaviour
{
// public member eines Scripts können bequem
// im Unity Editor gesetzt und auch während
// das Spiel getestet wird verändert werden.
@Keldrik
Keldrik / UnityCameraFollowObject.cs
Last active March 9, 2023 22:55
Unity Script: Smooth Follow and LookAt Object Behavior for Camera
using UnityEngine;
using System.Collections;
public class CameraFollowObject : MonoBehaviour
{
public Transform TargetObject;
public float MoveSmoothTime = 0.3F;
public float RotationSpeed = 6f;
public Vector3 Offset = new Vector3(0f, 15f, 0f);
@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 {