I been doing a lot of research online and can`t figure out how to do this.
I want to use multiple sprites as my rotation, how would i implement this into a game.
How the gun turns there all different sprites, not physically moving,
How would i implement this with lets say a rotating collider box for a car.
I need some advice how to do this, i tried this method with a car and it works fine with
sprites changing to the X, Y values. The only problem is when i press a opposite key or button
lets say “I press A to go left” if i press D, the sprite will flip in that direction and i don`t want this to happen
same for W to S, or S to W. or NE, TO SE.
Any advice is welcomed to solve this, like i said with a rotating box collider it will rotate the image and thats not
what i want to do. Maybe i`m making a big deal out of something so easy to do and there is a easier method.
Unless I’m missing something: Order your sprites in an array or list. Divide the angle in degrees by the number of elements in that list and floor it to get an index. Set the sprite to list[index].
2 Likes
I seem to have done it now, but with a minor problem.
If you are going Right = D and press Left = A, and press Down or up just for a tap
it will flip the car.
Is there a workaround ?
if (startcar)
{
x = Input.GetAxis("Horizontal");
y = Input.GetAxis("Vertical");
speedinput = 0;
moveDir = new Vector3(x, y).normalized;
if (x != 0 || y != 0)
{
if (x > .0 && !Left)
{
anim.SetFloat("X", x);
speedinput = moveSpeed * 100f;
moving = true;
anim.SetBool("isMoving", moving);
Up = false;
Down = false;
Left = false;
Right = true;
anim.SetFloat("X", x);
anim.SetFloat("Y", y);
}
if (x < -.0 && !Right)
{
anim.SetFloat("X", x);
speedinput = moveSpeed * 100f;
moving = true;
anim.SetBool("isMoving", moving);
Up = false;
Down = false;
Left = true;
Right = false;
anim.SetFloat("X", x);
anim.SetFloat("Y", y);
}
if (y > .0 && !Down)
{
anim.SetFloat("Y", y);
speedinput = moveSpeed * 100f;
moving = true;
anim.SetBool("isMoving", moving);
Up = true;
Down = false;
Left = false;
Right = false;
anim.SetFloat("X", x);
anim.SetFloat("Y", y);
if (x < -.0 && Up)
{
//anim.SetFloat("X", x);
speedinput = moveSpeed * 100f;
moving = true;
anim.SetBool("isMoving", moving);
Up = true;
Down = false;
}
if (x > .0 && Up)
{
//anim.SetFloat("X", x);
speedinput = moveSpeed * 100f;
moving = true;
anim.SetBool("isMoving", moving);
Up = true;
Down = false;
}
}
if (y < -.0 && !Up)
{
anim.SetFloat("Y", y);
speedinput = moveSpeed * 100f;
moving = true;
anim.SetBool("isMoving", moving);
Up = false;
Down = true;
Left = false;
Right = false;
anim.SetFloat("X", x);
anim.SetFloat("Y", y);
if (x < -.0 && Up)
{
//anim.SetFloat("X", x);
speedinput = moveSpeed * 100f;
moving = true;
anim.SetBool("isMoving", moving);
Up = true;
Down = false;
}
if (x > .0 && Up)
{
//anim.SetFloat("X", x);
speedinput = moveSpeed * 100f;
moving = true;
anim.SetBool("isMoving", moving);
Up = true;
Down = false;
}
}
}
else
{
Invoke("StopMoving", 1f);
}
if (moving == true && !sfxcarisplaying)
{
SFXCAR();
}
if (moving == false && sfxcarisplaying)
{
sfxcarisplaying = false;
FindObjectOfType<AudioManager>().Stop("SFX_Car_Engine");
}
}
Here’s something that quantizes an analog X/Y input into any number of steps. It is set up for 8 but that is just a variable, so you could set it to whatever you like and use the result to look up your sprite table.
Full package enclosed with demo, but here’s the script:
using UnityEngine;
using System.Collections;
public class QuantizeAngles : MonoBehaviour
{
public int NumAngles = 8;
public Transform RawSpinner;
public Transform QuantizedSpinner;
void Update ()
{
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
h = -h; // invert to match controller orientation
// get the raw angle, in radians
float angle = Mathf.Atan2 (h, v);
// up to degrees
float degrees = angle * 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 / NumAngles;
// make sure we're positive
degrees += 360;
// back up half a pie wedge
degrees += pieWedge / 2;
// get a quantized number
degrees = (int)(degrees / pieWedge);
// at this point here the degrees variable has an integer
// from 0 to N-1 (useful for pre-rotated sprite lookup)
// expand by original pie to return to angles
degrees = degrees * pieWedge;
// make sure 0 to 360, just for fun (not necessary really)
degrees = degrees % 360;
QuantizedSpinner.rotation = Quaternion.Euler( 0, 0, degrees);
}
}
7395671–903254–QuantizedAngles.unitypackage (4.79 KB)
1 Like
Thank you for the script, but its not what i need.
This script rotates a box collider, mine does not rotate as it swaps sprites out
and changes box collider to fit the sprite. Its a pixel art game so i want to keep pixel art aesthetic.
The script in my last post works like a charm, but if i press A while pressing S and press down or up
it will still flip the car, i dont want this to happen.

I beg to differ. Did you read the comment on lines 39 and 40? IF you DELETE the subsequent code and instead use that degrees AFTER the line 37 computation, it is actually an index from 0 (north) going around to N - 1, which you would use to look up one of your pre-rotated sprites. In your case, no actual rotation would be done.
In any case, I edited the script a bit to clear things up more. In the following script you would use the variable wedgeNumber to look up your sprite:
using UnityEngine;
using System.Collections;
public class QuantizeAngles : MonoBehaviour
{
public int NumStepsAroundCircle = 8;
public Transform RawSpinner;
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
// 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);
// at this point here the wedgeNumber variable has an integer
// from 0 to N-1 (useful for pre-rotated sprite lookup)
// 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);
}
}