Skip to content

Instantly share code, notes, and snippets.

@uni-bbl
Created March 2, 2016 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uni-bbl/562b7cd3fa28a09af767 to your computer and use it in GitHub Desktop.
Save uni-bbl/562b7cd3fa28a09af767 to your computer and use it in GitHub Desktop.
Many Unity developers create a convenience base class to reuse pervasive idioms. Here's mine. (delete the Photon stuff if you don't need it)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class XBehaviour : MonoBehaviour {
private const float GROUND_THRESHOLD = 0.1f;
private Velocity velocityComponent;
public float altitude {
get{
RaycastHit hit;
float h = height/2f;
Vector3 C = position+up*h;
bool didHit = Physics.Raycast(C, down, out hit);
if(didHit){
Debug.DrawLine(position, hit.point, Color.green);
return position.y - hit.point.y;
}else{
Debug.DrawRay(position, down*10f, Color.red);
return 1e6f;
}
}
}
public Rigidbody body{
get{ return GetComponent<Rigidbody> (); }
}
public bool grounded{
get{
return altitude<GROUND_THRESHOLD;
}
}
public float height{
get{
return GetComponentInChildren<Collider>().bounds.size.y;
}
}
public Vector3 position{
get{ return transform.position; }
set{ transform.position=value; }
}
public Quaternion rotation{
get{return transform.rotation; }
set{transform.rotation = value; }
}
public float speed{
get{
return velocity.magnitude;
}
}
public float groundSpeed{
get{
Vector3 v = velocity;
v.y=0f;
return v.magnitude;
}
}
public Vector3 velocity{
get{
if (velocityComponent == null) {
velocityComponent = GetComponent<Velocity>();
if(velocityComponent==null){
velocityComponent = gameObject.AddComponent<Velocity>();
}
return Vector3.zero;
}
return velocityComponent.velocity;
}
}
// Protected properties ----------------------------------------------------------------
protected Vector3 up { get{ return Vector3.up; } }
protected Vector3 down { get{ return Vector3.down; } }
protected Vector3 right { get{ return Vector3.right; } }
protected Vector3 left { get{ return Vector3.left; } }
protected Vector3 forth { get{ return Vector3.forward; } }
protected Vector3 back { get{ return Vector3.back; } }
// Public functions --------------------------------------------------------------------
public Transform FindBone(string n,Transform parent = null){
if(parent==null){parent=transform;}
if(parent.gameObject.name==n)return parent;
foreach(Transform child in parent){
Transform c = FindBone(n,child);
if(c!=null)return c;
}
return null;
}
public List<T> All<T>(float range=1f) where T:Component{
List<T> output = new List<T>();
T[] objects = Object.FindObjectsOfType<T>();
foreach (T x in objects) {
if(isAncestorOf(x))continue;
float d = Distance (x);
if(d <= range){
output.Add (x);
}
}
return output;
}
/**
* Returns the position of the nearest object which
* has a component of type T.
*/
public Vector3 Position<T>() where T:Component{
return This<T> ().transform.position;
}
/**
* Returns the component of type T attached to the nearest object
* which has such a component.
*/
public T This<T>() where T:Component{
T[] objects = Object.FindObjectsOfType<T>();
float distance = 0f;
T value = null;
foreach (T x in objects) {
if(isAncestorOf(x))continue;
float d = Distance (x);
if(d<distance || value==null){
distance=d;
value=x;
}
}
return value;
}
// Functions ----------------------------------------------------------------------------------
public float Distance(GameObject that){ return (that.transform.position-position).magnitude; }
public float Distance(Component c){ return (c.transform.position-position).magnitude; }
public float Distance(Vector3 that){ return (that-position).magnitude; }
public Vector3 Direction(Component that){ return (that.transform.position-position).normalized; }
public Vector3 Direction(GameObject that){ return (that.transform.position-position).normalized; }
public Vector3 Direction(Vector3 P){ return (P-position).normalized; }
private bool isAncestorOf(Component c){
Transform t = c.transform;
while (t!=null) {
if (t == this.transform) return true;
t=t.parent;
}
return false;
}
protected PhotonView photonView{get{return GetComponent<PhotonView>();}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment