Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- // *************
- // Script based on ideas from https://connect.unity.com/p/articles-what-i-learned-from-trying-to-make-an-isometric-game-in-unity
- // *************
- [ExecuteInEditMode]
- [RequireComponent(typeof(Renderer))]
- public class SpriteOrderer : MonoBehaviour {
- [SerializeField]
- [Tooltip("Where the sprite would be touching the ground if it were a 3D object")]
- private float floorHeight = 0f;
- [SerializeField]
- [Tooltip("If this object is never going to move in-game, this can save a small amount of overhead")]
- private bool isStatic = false;
- private new Renderer renderer;
- void Start()
- {
- renderer = GetComponent<Renderer>();
- }
- void OnEnable()
- {
- renderer = GetComponent<Renderer>();
- }
- void Update ()
- {
- // Some objects never move in the game, so we can save a bit of overhead
- if (isStatic && Application.isPlaying)
- return;
- // In case where renderer was added after script loaded, try to cache it again (for Edit Mode)
- if (renderer == null)
- renderer = GetComponent<Renderer>();
- Vector3 bottomLeft = renderer.bounds.min + (Vector3.up * floorHeight);
- Vector3 bottomRight = renderer.bounds.min + (Vector3.right * renderer.bounds.size.x) + (Vector3.up * floorHeight);
- float yPosWithFloor = ((bottomLeft + bottomRight) / 2).y;
- Vector3 newPos = transform.position;
- newPos.z = yPosWithFloor * 0.01f;
- transform.position = newPos;
- }
- void OnDrawGizmos()
- {
- Gizmos.color = Color.yellow;
- Vector3 bottomLeft = renderer.bounds.min + (Vector3.up * floorHeight);
- Vector3 bottomRight = renderer.bounds.min + (Vector3.right * renderer.bounds.size.x) + (Vector3.up * floorHeight);
- Gizmos.DrawLine(bottomLeft, bottomRight);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement