Skip to content

Instantly share code, notes, and snippets.

@NovaSurfer
Last active November 5, 2016 18:52
Show Gist options
  • Save NovaSurfer/329572688974a7845372580a2e895a35 to your computer and use it in GitHub Desktop.
Save NovaSurfer/329572688974a7845372580a2e895a35 to your computer and use it in GitHub Desktop.
Unity3d detecting collision without physics
/*
Tutorial (russian):
http://nodejso-tribar.rhcloud.com/collison-without-physics/
*/
using UnityEngine;
using System.Collections;
public class BoundsCollider : DrawBoxCollider {
public Renderer targetRend;
void Update()
{
if (currentRenderer.bounds.Intersects(targetRend.bounds))
{
gizmoColor = Color.red;
}
else
{
gizmoColor = Color.cyan;
}
}
}
/*
Tutorial (russian):
http://nodejso-tribar.rhcloud.com/collison-without-physics/
*/
using UnityEngine;
using System.Collections;
[ExecuteInEditMode] // Необязательно, но OnDrawGizmos() будет кидать null reference exception в редакторе
public abstract class DrawBounds : MonoBehaviour {
protected Renderer currentRenderer;
protected Color gizmoColor = Color.cyan;
protected virtual void Awake()
{
currentRenderer = GetComponent<Renderer>();
}
private void OnDrawGizmos()
{
Vector3 center = currentRenderer.bounds.center;
Vector3 size = currentRenderer.bounds.size;
Gizmos.color = gizmoColor;
Gizmos.DrawWireCube(center, size);
}
}
/*
Tutorial (russian):
http://nodejso-tribar.rhcloud.com/collison-without-physics/
*/
using UnityEngine;
using System.Collections;
public class DrawBoxCollider : DrawBounds {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment