Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Rotate with sprites

Discussion in 'Scripting' started by Mattyerial, Oct 2, 2021.

  1. Mattyerial

    Mattyerial

    Joined:
    Oct 5, 2018
    Posts:
    51
    16_angle_pixelart_gun_animation_rotate.gif 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.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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].
     
    Olmi and Kurt-Dekker like this.
  3. Mattyerial

    Mattyerial

    Joined:
    Oct 5, 2018
    Posts:
    51
    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 ?


    Code (CSharp):
    1.  if (startcar)
    2.         {
    3.  
    4.             x = Input.GetAxis("Horizontal");
    5.             y = Input.GetAxis("Vertical");
    6.             speedinput = 0;
    7.  
    8.             moveDir = new Vector3(x, y).normalized;
    9.  
    10.             if (x != 0 || y != 0)
    11.             {
    12.  
    13.                 if (x > .0 && !Left)
    14.                 {
    15.                     anim.SetFloat("X", x);
    16.                     speedinput = moveSpeed * 100f;
    17.                     moving = true;
    18.                     anim.SetBool("isMoving", moving);
    19.                     Up = false;
    20.                     Down = false;
    21.                     Left = false;
    22.                     Right = true;
    23.                     anim.SetFloat("X", x);
    24.                     anim.SetFloat("Y", y);
    25.                 }
    26.  
    27.                 if (x < -.0 && !Right)
    28.                 {
    29.                     anim.SetFloat("X", x);
    30.                     speedinput = moveSpeed * 100f;
    31.                     moving = true;
    32.                     anim.SetBool("isMoving", moving);
    33.                     Up = false;
    34.                     Down = false;
    35.                     Left = true;
    36.                     Right = false;
    37.                     anim.SetFloat("X", x);
    38.                     anim.SetFloat("Y", y);
    39.                 }
    40.  
    41.                 if (y > .0 && !Down)
    42.                 {
    43.                     anim.SetFloat("Y", y);
    44.                     speedinput = moveSpeed * 100f;
    45.                     moving = true;
    46.                     anim.SetBool("isMoving", moving);
    47.                     Up = true;
    48.                     Down = false;
    49.                     Left = false;
    50.                     Right = false;
    51.                     anim.SetFloat("X", x);
    52.                     anim.SetFloat("Y", y);
    53.  
    54.  
    55.                     if (x < -.0 && Up)
    56.                     {
    57.                         //anim.SetFloat("X", x);
    58.                         speedinput = moveSpeed * 100f;
    59.                         moving = true;
    60.                         anim.SetBool("isMoving", moving);
    61.                         Up = true;
    62.                         Down = false;
    63.                     }
    64.  
    65.                     if (x > .0 && Up)
    66.                     {
    67.                         //anim.SetFloat("X", x);
    68.                         speedinput = moveSpeed * 100f;
    69.                         moving = true;
    70.                         anim.SetBool("isMoving", moving);
    71.                         Up = true;
    72.                         Down = false;
    73.                     }
    74.                 }
    75.  
    76.                 if (y < -.0 && !Up)
    77.                 {
    78.                     anim.SetFloat("Y", y);
    79.                     speedinput = moveSpeed * 100f;
    80.                     moving = true;
    81.                     anim.SetBool("isMoving", moving);
    82.                     Up = false;
    83.                     Down = true;
    84.                     Left = false;
    85.                     Right = false;
    86.                     anim.SetFloat("X", x);
    87.                     anim.SetFloat("Y", y);
    88.  
    89.                     if (x < -.0 && Up)
    90.                     {
    91.                         //anim.SetFloat("X", x);
    92.                         speedinput = moveSpeed * 100f;
    93.                         moving = true;
    94.                         anim.SetBool("isMoving", moving);
    95.                         Up = true;
    96.                         Down = false;
    97.                     }
    98.  
    99.                     if (x > .0 && Up)
    100.                     {
    101.                         //anim.SetFloat("X", x);
    102.                         speedinput = moveSpeed * 100f;
    103.                         moving = true;
    104.                         anim.SetBool("isMoving", moving);
    105.                         Up = true;
    106.                         Down = false;
    107.                     }
    108.                 }
    109.             }
    110.             else
    111.             {
    112.                 Invoke("StopMoving", 1f);
    113.             }
    114.  
    115.             if (moving == true && !sfxcarisplaying)
    116.             {
    117.                 SFXCAR();
    118.             }
    119.  
    120.             if (moving == false && sfxcarisplaying)
    121.             {
    122.                 sfxcarisplaying = false;
    123.                 FindObjectOfType<AudioManager>().Stop("SFX_Car_Engine");
    124.             }
    125.         }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    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:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class QuantizeAngles : MonoBehaviour
    5. {
    6.     public int    NumAngles = 8;
    7.  
    8.     public Transform RawSpinner;
    9.     public Transform QuantizedSpinner;
    10.  
    11.     void Update ()
    12.     {
    13.         float h = Input.GetAxis ("Horizontal");
    14.         float v = Input.GetAxis ("Vertical");
    15.  
    16.         h = -h;        // invert to match controller orientation
    17.  
    18.         // get the raw angle, in radians
    19.         float angle = Mathf.Atan2 (h, v);
    20.  
    21.         // up to degrees
    22.         float degrees = angle * Mathf.Rad2Deg;
    23.  
    24.         // drive the raw spinner
    25.         RawSpinner.rotation = Quaternion.Euler( 0, 0, degrees);
    26.  
    27.         // how big is a pie wedge in degrees?
    28.         float pieWedge = 360.0f / NumAngles;
    29.  
    30.         // make sure we're positive
    31.         degrees += 360;
    32.  
    33.         // back up half a pie wedge
    34.         degrees += pieWedge / 2;
    35.  
    36.         // get a quantized number
    37.         degrees = (int)(degrees / pieWedge);
    38.  
    39. // at this point here the degrees variable has an integer
    40. // from 0 to N-1 (useful for pre-rotated sprite lookup)
    41.  
    42.         // expand by original pie to return to angles
    43.         degrees = degrees * pieWedge;
    44.  
    45.         // make sure 0 to 360, just for fun (not necessary really)
    46.         degrees = degrees % 360;
    47.  
    48.         QuantizedSpinner.rotation = Quaternion.Euler( 0, 0, degrees);
    49.     }
    50. }
     

    Attached Files:

    GroZZleR likes this.
  5. Mattyerial

    Mattyerial

    Joined:
    Oct 5, 2018
    Posts:
    51
    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.
     

    Attached Files:

  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    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:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class QuantizeAngles : MonoBehaviour
    5. {
    6.     public int    NumStepsAroundCircle = 8;
    7.  
    8.     public Transform RawSpinner;
    9.     public Transform QuantizedSpinner;
    10.  
    11.     void Update ()
    12.     {
    13.         // get h and v however you like
    14.         float h = Input.GetAxis ("Horizontal");
    15.         float v = Input.GetAxis ("Vertical");
    16.  
    17.         h = -h;        // invert to match controller orientation
    18.  
    19.         // get the raw angle, in radians
    20.         float radians = Mathf.Atan2 (h, v);
    21.  
    22.         // up to degrees
    23.         float degrees = radians * Mathf.Rad2Deg;
    24.  
    25.         // drive the raw spinner
    26.         RawSpinner.rotation = Quaternion.Euler( 0, 0, degrees);
    27.  
    28.         // how big is a pie wedge in degrees?
    29.         float pieWedge = 360.0f / NumStepsAroundCircle;
    30.  
    31.         // make sure we're positive
    32.         degrees += 360;
    33.  
    34.         // back up half a pie wedge
    35.         degrees += pieWedge / 2;
    36.  
    37.         // get a quantized number
    38.         int wedgeNumber = (int)(degrees / pieWedge);
    39.  
    40. // at this point here the wedgeNumber variable has an integer
    41. // from 0 to N-1 (useful for pre-rotated sprite lookup)
    42.  
    43.         // expand by original pie to return to degrees
    44.         degrees = wedgeNumber * pieWedge;
    45.  
    46.         // make sure 0 to 360, just for fun (not necessary really)
    47.         degrees = degrees % 360;
    48.  
    49.         QuantizedSpinner.rotation = Quaternion.Euler( 0, 0, degrees);
    50.     }
    51. }