Last active
November 5, 2016 18:52
-
-
Save NovaSurfer/329572688974a7845372580a2e895a35 to your computer and use it in GitHub Desktop.
Unity3d detecting collision without physics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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