How can we make a script to changer render order, based on players and enemies Y positions?
I need my enemy to switch to the layer behind my player when above him, and vice versa. My players rendering order is 3. Been trying for a few hours now and would really appreciate some help!
using UnityEngine;
public class EnemyLayerSwitcher : MonoBehaviour
{
public GameObject player;
GameObject Enemy;
public int sortingOrder = 0;
private SpriteRenderer enemylayer;
float playersY = 0;
float enemysY = 0;
private void Start()
{
Enemy = GameObject.FindWithTag("Enemy");
enemylayer = Enemy.GetComponent<SpriteRenderer>();
}
void Update()
{
float playersY = player.transform.position.y;
float enemysY = Enemy.transform.position.x;
Debug.Log(enemysY);
changelayer();
}
void changelayer()
{
if (playersY > enemysY)
{ enemylayer.sortingOrder = 2; }
else
{ enemylayer.sortingOrder = 3; }
}
}
Thanks in advance
Comment