Is there a way to prevent diagonal movement from rotating a sprite two directions at the same time?

Hello,
I have a top-down game where a player sprite can be moved in 8 directions, and it has another sprite “attached” to it that moves with the same controls that the player uses. When I move the player diagonally in all 4 directions it moves just fine, being smooth and keeping up with the correct speed. However, the second sprite that moves with it acts the opposite. Whenever I move it diagonally, it results in a very jittery motion that does not keep up with the player. However, the moment I let go of one of the diagonal keys, it teleports onto the player and moves just fine.

After messing around to find the problem, I discovered that it has to do with rotation. When I move the second sprite up/down I rotate it 90 degrees so it looks like its looking up, and when its moving left/right the rotation reverts to 0 degrees. My code detects that when the sprite movement speed is greater than 0 for both horizontal and vertical movement, then it rotates the sprite correspondingly. If I remove one of these checks, then the sprite moves just fine. Correct me if I am wrong, but I think that because it finds that the horizontal and vertical movement speed is greater than 0 at the same time when moving diagonally, then the game gets stuck between constantly rotating the sprite 0 and 90 degrees. Is there a way where the game can prevent the two rotations to happen at the same time when moving diagonally? I really would like to keep the rotation function on the sprite if possible.

Here is my code for the second sprite:

public class RotationMovement : MonoBehaviour
{
    public float vertical;
    public float horizontal;
    public float speedLimit = 0.75f;
    public float moveSpeed;
    [SerializeField] private Rigidbody2D rb;
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");
        vertical = Input.GetAxisRaw("Vertical");

        if (horizontal > 0f)
        {
            rb.velocity = new Vector2(horizontal * moveSpeed, rb.velocity.y);
            transform.localScale = new Vector3(1, 1.5f, 0);
            transform.localRotation = Quaternion.Euler(0, 0, 0);
            GetComponent<BoxCollider2D>().offset = new Vector2(0.9f, 0);
        }
        else if (horizontal < 0f)
        {
            rb.velocity = new Vector2(horizontal * moveSpeed, rb.velocity.y);
            transform.localScale = new Vector3(-1, -1.5f, 0);
            transform.localRotation = Quaternion.Euler(0, 0, 0);
            GetComponent<BoxCollider2D>().offset = new Vector2(1, 0);
        }
        else
        {
            rb.velocity = new Vector2(0, rb.velocity.y);
        }

        if (vertical > 0f)
        {
            transform.localScale = new Vector3(1, 1.5f, 0);
            transform.localRotation = Quaternion.Euler(0, 0, 90);
        }
        if (vertical < 0f)
        {
            transform.localScale = new Vector3(1, -1.5f, 0);
            transform.localRotation = Quaternion.Euler(0, 0, -90);
        }
    }

    void FixedUpdate()
    {
        if (horizontal != 0 && vertical != 0)
        {
            horizontal *= speedLimit;
            vertical *= speedLimit;
        }

        rb.velocity = new Vector2(horizontal * moveSpeed, vertical * moveSpeed);
    }
}

Here is a video that shows what happens with the diagonal movement. The white rectangle is the sprite that is having issues:

Your script doesn’t need to exist. To make an object appear to carry another object you should just make the object to be carried a child of the carrier. But before carrying/parenting an object you’ll need to remove its rigidbody (if it has one) so then it can no longer move around on its own.

Then in your player movement script you can rotate the carried child object (white rectangle) when the player changes direction.

I get what you’re saying, but the problem is that when I make the sprite a child, it does not rotate the way that I need it to. Instead, it stretches an attached boxcollider upwards when I move up and down, which makes it lose its original shape. The only way I got it to keep its shape is if I make it its own sprite.

After a bit of testing, I did manage to find a way to make it work, thank you for the help though :slight_smile: