Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- // Needed for changing Layer Order
- using UnityEngine.Rendering;
- public class ActorPosition : MonoBehaviour
- {
- // References
- public SortingGroup actorController;
- public SortingGroup actorShadow;
- public float ActorPositionVector = 0f;
- public float FloorTransitionVector = 0f;
- public int actorFloor = 1;
- public int actorLayerOrder = 200;
- public int actorShadowOrder = 140;
- void Start()
- {
- // Setting up references for Sorting Order
- actorController = transform.Find("actor_animator_controller").GetComponent<SortingGroup>();
- actorShadow = transform.Find("actor_shadow").GetComponent<SortingGroup>();
- sortingGroupUpdate();
- }
- void OnTriggerStay2D(Collider2D other)
- {
- if (other == null) return;
- if (other.GetComponentInParent<FloorSorting>() != null) {
- if (actorFloor < other.GetComponent<FloorSorting>().FloorNumber){
- if (actorFloor != other.GetComponent<FloorSorting>().FloorNumber){
- ActorPositionVector = this.transform.position.y;
- FloorTransitionVector = other.GetComponent<Collider2D>().bounds.center.y;
- // FloorTransitionVector = other.GetComponent<Collider2D>().transform.position.y + other.GetComponent<Collider2D>().offset.y;
- Debug.Log("Raising Actor Draw Order.");
- actorFloor = other.GetComponent<FloorSorting>().FloorNumber;
- sortingGroupUpdate();
- }
- }
- }
- }
- void OnTriggerExit2D(Collider2D other) {
- if (other.GetComponentInParent<FloorSorting>() != null) {
- Debug.Log("EXITING Floor COL");
- ActorPositionVector = this.transform.position.y;
- FloorTransitionVector = other.GetComponent<Collider2D>().bounds.center.y;
- // FloorTransitionVector = other.GetComponent<Collider2D>().transform.position.y + other.GetComponent<Collider2D>().offset.y;
- if (ActorPositionVector <= FloorTransitionVector){
- Debug.Log("Lowering Actor Draw Order.");
- actorFloor = other.GetComponent<FloorSorting>().FloorNumber - 1;
- sortingGroupUpdate();
- }
- }
- }
- void sortingGroupUpdate(){
- // Moving Actor's model and Shadow to new Sorting Orders.
- actorLayerOrder = 100 + actorFloor*100;
- actorShadowOrder = 40 + actorFloor*100;
- actorController.sortingOrder = actorLayerOrder;
- actorShadow.sortingOrder = actorShadowOrder;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement