Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. // Needed for changing Layer Order
  5. using UnityEngine.Rendering;
  6.  
  7. public class ActorPosition : MonoBehaviour
  8. {
  9.  
  10. // References
  11. public SortingGroup actorController;
  12. public SortingGroup actorShadow;
  13.  
  14. public float ActorPositionVector = 0f;
  15. public float FloorTransitionVector = 0f;
  16.  
  17. public int actorFloor = 1;
  18.  
  19. public int actorLayerOrder = 200;
  20. public int actorShadowOrder = 140;
  21.  
  22.  
  23.  
  24. void Start()
  25. {
  26. // Setting up references for Sorting Order
  27. actorController = transform.Find("actor_animator_controller").GetComponent<SortingGroup>();
  28. actorShadow = transform.Find("actor_shadow").GetComponent<SortingGroup>();
  29.  
  30.  
  31. sortingGroupUpdate();
  32. }
  33.  
  34.  
  35. void OnTriggerStay2D(Collider2D other)
  36. {
  37. if (other == null) return;
  38.  
  39. if (other.GetComponentInParent<FloorSorting>() != null) {
  40.  
  41. if (actorFloor < other.GetComponent<FloorSorting>().FloorNumber){
  42.  
  43. if (actorFloor != other.GetComponent<FloorSorting>().FloorNumber){
  44.  
  45. ActorPositionVector = this.transform.position.y;
  46. FloorTransitionVector = other.GetComponent<Collider2D>().bounds.center.y;
  47. // FloorTransitionVector = other.GetComponent<Collider2D>().transform.position.y + other.GetComponent<Collider2D>().offset.y;
  48.  
  49. Debug.Log("Raising Actor Draw Order.");
  50.  
  51. actorFloor = other.GetComponent<FloorSorting>().FloorNumber;
  52. sortingGroupUpdate();
  53. }
  54.  
  55. }
  56. }
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63. void OnTriggerExit2D(Collider2D other) {
  64. if (other.GetComponentInParent<FloorSorting>() != null) {
  65.  
  66. Debug.Log("EXITING Floor COL");
  67.  
  68. ActorPositionVector = this.transform.position.y;
  69. FloorTransitionVector = other.GetComponent<Collider2D>().bounds.center.y;
  70. // FloorTransitionVector = other.GetComponent<Collider2D>().transform.position.y + other.GetComponent<Collider2D>().offset.y;
  71.  
  72. if (ActorPositionVector <= FloorTransitionVector){
  73. Debug.Log("Lowering Actor Draw Order.");
  74.  
  75. actorFloor = other.GetComponent<FloorSorting>().FloorNumber - 1;
  76. sortingGroupUpdate();
  77.  
  78. }
  79.  
  80. }
  81.  
  82. }
  83.  
  84.  
  85. void sortingGroupUpdate(){
  86.  
  87. // Moving Actor's model and Shadow to new Sorting Orders.
  88. actorLayerOrder = 100 + actorFloor*100;
  89. actorShadowOrder = 40 + actorFloor*100;
  90.  
  91. actorController.sortingOrder = actorLayerOrder;
  92. actorShadow.sortingOrder = actorShadowOrder;
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement