Advertisement
Guest User

Untitled

a guest
May 16th, 2018
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. // *************
  4. // Script based on ideas from https://connect.unity.com/p/articles-what-i-learned-from-trying-to-make-an-isometric-game-in-unity
  5. // *************
  6.  
  7. [ExecuteInEditMode]
  8. [RequireComponent(typeof(Renderer))]
  9. public class SpriteOrderer : MonoBehaviour {
  10.  
  11.     [SerializeField]
  12.     [Tooltip("Where the sprite would be touching the ground if it were a 3D object")]
  13.     private float floorHeight = 0f;
  14.  
  15.     [SerializeField]
  16.     [Tooltip("If this object is never going to move in-game, this can save a small amount of overhead")]
  17.     private bool isStatic = false;
  18.  
  19.     private new Renderer renderer;
  20.  
  21.     void Start()
  22.     {
  23.         renderer = GetComponent<Renderer>();
  24.     }
  25.  
  26.     void OnEnable()
  27.     {
  28.         renderer = GetComponent<Renderer>();
  29.     }
  30.  
  31.     void Update ()
  32.     {
  33.         // Some objects never move in the game, so we can save a bit of overhead
  34.         if (isStatic && Application.isPlaying)
  35.             return;
  36.  
  37.         // In case where renderer was added after script loaded, try to cache it again (for Edit Mode)
  38.         if (renderer == null)
  39.             renderer = GetComponent<Renderer>();
  40.  
  41.         Vector3 bottomLeft = renderer.bounds.min + (Vector3.up * floorHeight);
  42.         Vector3 bottomRight = renderer.bounds.min + (Vector3.right * renderer.bounds.size.x) + (Vector3.up * floorHeight);
  43.         float yPosWithFloor = ((bottomLeft + bottomRight) / 2).y;
  44.         Vector3 newPos = transform.position;
  45.  
  46.         newPos.z = yPosWithFloor * 0.01f;
  47.         transform.position = newPos;
  48.     }
  49.  
  50.     void OnDrawGizmos()
  51.     {
  52.         Gizmos.color = Color.yellow;
  53.  
  54.         Vector3 bottomLeft = renderer.bounds.min + (Vector3.up * floorHeight);
  55.         Vector3 bottomRight = renderer.bounds.min + (Vector3.right * renderer.bounds.size.x) + (Vector3.up * floorHeight);
  56.  
  57.         Gizmos.DrawLine(bottomLeft, bottomRight);
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement