Don’t mind this part, I just wanted to see if this kind of code can be super-streamlined
var direction = (thisPos - prevPos).normalized;
var angle = Mathf.Atan2(direction.y, direction.x); // in radians
var rotation = Quaternion.AngleAxis(angle * Mathf.Rad2Deg, Vector3.forward);
somePart[i].transform.localRotation = rotation;
It turns out it can
// using System;
var ha = MathF.Atan2(thisPos.y - prevPos.y, thisPos.x - prevPos.x) * .5f;
somePart[i].transform.localRotation = new Quaternion(0f, 0f, MathF.Sin(ha), MathF.Cos(ha));
Quaternion is built via trigonometry anyways, so we can avoid normalization.
What’s interesting (to me, ymmv) is that skipping doing the angle should also work because of half-angle identities, but then we need more square roots (I haven’t tested this variant though, might do it later)
var d = thisPos - prevPos;
var idm = .5f * sqrt(d.x * d.x + d.y * d.y); // inverse double magnitude
var cha = sign(d.x) * sqrt(idm * (d.y + d.x));
var sha = sign(d.y) * sqrt(idm * (d.y - d.x));
somePart[i].transform.localRotation = new Quaternion(0f, 0f, sha, cha);
Edit: This variant also allows one to extract direction for free.
var d = thisPos - prevPos;
var im = 1f / sqrt(d.x * d.x + d.y * d.y);
var dir = d * im;
var cha = sign(d.x) * sqrt(2f * im * (d.y + d.x));
var sha = sign(d.y) * sqrt(2f * im * (d.y - d.x));
somePart[i].transform.localRotation = new Quaternion(0f, 0f, sha, cha);
Edit2: I should probably normalize d components before using them, have to try this, will fix later.