I have a 2d ortho camera game where I have a donut shape sprite(portal) rotated on the X axis and cut in half, with each half having its own GameObject/spriterenderer.
This is so the player character can appear in the middle of the portal object (like in the middle of a donut hole). I have the back side with the sorting layer as background and the front side as the sorting layer foreground object (in front of player sprite).
Now this would be great if this portal was supposed to be static but I want it to rotate (which i’m doing with DoTween)
However, obviously once the sprites rotate around, their sorting layer needs to be flipped… because currently this is the outcome without accounting for updating their sorting layer
Video:
Heres a rotationcheck script i was trying to implement but it doesn’t work at all. I basically would need it to check after every 180 degrees turned on the Z axis. I guess I could hard write the numbers now that I think about it? If anyone had suggestion or advice, please let me know. Would really appreciate it! Thanks
using UnityEngine;
public class RotationCheck : MonoBehaviour
{
private Quaternion initialRotation;
private SpriteRenderer spriteRenderer;
void Start()
{
// Get the SpriteRenderer component
spriteRenderer = GetComponent<SpriteRenderer>();
// Store the initial rotation as a Quaternion
initialRotation = transform.rotation;
}
void Update()
{
// Calculate the angle difference between the current and initial rotations
float angleDifference = Quaternion.Angle(initialRotation, transform.rotation);
// Check if the rotation has changed by at least 180 degrees
if (Mathf.Abs(angleDifference) >= 180f)
{
Debug.Log("called");
// Toggle between "ForegroundObject" and "Background" sorting layers
if (spriteRenderer.sortingLayerName == "ForegroundObject")
{
spriteRenderer.sortingLayerName = "Background";
}
else
{
spriteRenderer.sortingLayerName = "ForegroundObject";
}
// Update the initial rotation for the next check
initialRotation = transform.rotation;
}
}
}