Sorry for the generic title, but I’m struggling to describe what I’m trying to do summarized enough to fit in a title.
So! I’m making a top-down 3D game with 2D sprites, and the sprite is gonna look at where the mouse is pointed, I’ll do this by making the player an invisible cube that uses LookAt, or an alternative I haven’t settled on it yet, to rotate, and a child of that cube that decides what sprite to use based on the rotation of the cube, so I’ll need 360 sprites, or a divisor of it since 1 degree of rotation is probably impossible to convey in a 2D sprite.
Now, my issue is that I want this sprite animated, and animated differently based on where it’s looking, meaning 360(or divisor) walking animations, running animations, that will swap between each other, keeping track on how deep into the animation they are too, each frame when the player rotates, and will probably need a sprite conveniently lackign legs to avoid moonwalking but that’s another discussion lol.
[In hindsight, no matter what I’ll need to make movement animations directional anyway since, unless you literally not convey any sense of direction in the movement at all, you will get some degree of moonwalking no matter how the sprite looks]
So, my question about feasibility is about whether this would run, or if it’s too many things happening every frame and would lead to crashing/lagging/requiring unreasonably high specs.
You’re way overthinking this. Mock something up. Put down some terrain, put a camera about where you think it should be, drop a 2D quad sprite in and start moving it around.
If you cannot get what you want by placing stuff in the scene and rotating it by hand to show it off, it will be impossible to realize what you want: you need to use it to gel in your own mind what you’re doing, as well as to ask questions here.
Once you have that (should be about five minutes max) you can now make screencaps of it and draw on those screencaps to post here, with “Hey, I’m doing this, I want this sprite to point this way, how?” and go from there.
I don’t want the sprite to rotate, I want the sprite itself to always point towards the camera, while making the sprite change based on how it’s rotated*.
I wouldn’t wanna put all that busywork in making sprites to then find out that changing sprite multiple times per frame just crashes the game or similar, so I’d ask first if this roads leads anywhere.
*based on how the invisible parent gameObject is rotated
You can probably change the sprite a million times per frame and not have an issue.
What you contemplate is how ALL old games in the early 1980s were done.
I wouldn’t bother with invisible gameobjects and whatnot, just use Atan2() and be done with it.
Here’s how:
Use Mathf.Atan2() to derive heading from cartesian coordinates:
Sine/Cosine/Atan2 (sin/cos/atan2) and rotational familiarity and conventions
For something discrete like sprites you would want a reliable way to convert floating point angles (either radians or degrees) into X number of sprites going around. Here’s a package that demonstrates this basic numeric transformation, from a continuous angle to a discreet index number you could use for sprite lookup.
using UnityEngine;
public class QuantizeAngles : MonoBehaviour
{
[Header("How many steps around circle?")]
public int NumStepsAroundCircle = 8;
[Header("Raw - not quantized.")]
public Transform RawSpinner;
[Header("Quantized into steps.")]
public Transform QuantizedSpinner;
void Update ()
{
// get h and v however you like
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
h = -h; // invert to match controller orientation
// only do it if beyond a certain amount
Vector2 v2 = new Vector2(h, v);
if (v2.magnitude > 0.1f)
{
// get the raw angle, in radians
float radians = Mathf.Atan2(h, v);
// up to degrees
float degrees = radians * Mathf.Rad2Deg;
// drive the raw spinner
RawSpinner.rotation = Quaternion.Euler(0, 0, degrees);
// how big is a pie wedge in degrees?
float pieWedge = 360.0f / NumStepsAroundCircle;
// make sure we're positive
degrees += 360;
// back up half a pie wedge
degrees += pieWedge / 2;
// get a quantized number
int wedgeNumber = (int)(degrees / pieWedge);
Debug.Log(wedgeNumber);
// expand by original pie to return to degrees
degrees = wedgeNumber * pieWedge;
// make sure 0 to 360, just for fun (not necessary really)
degrees = degrees % 360;
QuantizedSpinner.rotation = Quaternion.Euler(0, 0, degrees);
}
}
}
Thank youu, I’ll stick with invisible gameObjects until I get a functional draft of a game, and then put this one in instead and see what changes
I figured that a fast way to learn differences between different methods of obtaining the same thing is to commit to one, change to the other and see what breaks lol
that is not at all what my name means but i do love the interpretation lmao
I’m waiting to make any significant progress in the game making before bothering with version control, it took me 6 hours to make lookAt work the way i wanted to, and it would take 5 minutes to do it again, the magic of learning. So anytime i fuck up too big, i just redo it from scratch, both because it doesn’t take nearly as long as the first time, and because this way i memorize better the basics of coding by doing it often instead of doing it once and never touching it again, which i reckon it’s pretty solid advice!