I need to animate a character sprite in a 3D environment.
So far, I have created a Quad, on which I have put a material containing the (possibly malfunctioning) sprite sheet/atlas (.png). I have also attached the script below to the Quad.
I have two major challenges:
-
The spritesheet refuses to show up as transparent in-game. Inspecting the .png-file shows the collection of sprites with the proper transparent areas. The background is either grey or a smear of colors.
-
I have modified the script below in many ways to change the quad’s current sprite by using UV-coordinates, but I can’t figure out how to make this work properly. As of now, nothing changes in-game no matter what I set the UV-coordinates to.
Any ideas on where I might’ve gone wrong? I must’ve read 100 articles on UV-mapping by now.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UVQuadMapping : MonoBehaviour
{
public Rect face = new Rect(0f, 0f, 1f, 1f);
private float _elapsedTime = 0;
private Mesh mesh;
private new Vector2[] UVs;
private int faceIndex = 0;
void Start()
{
mesh = transform.GetComponent<MeshFilter>().mesh as Mesh;
Vector2[] UVs = new Vector2[mesh.uv.Length];
UVs = mesh.uv;
setUVs(faceIndex);
mesh.uv = UVs;
}
void Update()
{
_elapsedTime += Time.deltaTime;
if(_elapsedTime > 1f)
_elapsedTime = 0;
else
return;
setUVs(faceIndex++);
mesh.uv = UVs;
}
void setUVs(int pOffset = 0)
{
UVs[0] = new Vector2(0, 0);
UVs[1] = new Vector2(1, 0);
UVs[2] = new Vector2(0, 1);
UVs[3] = new Vector2(1, 1);
}
}
Thanks! Will gladly add more details if needed.