Problem in 2D shooter help

Hi guys im currently trying to make a 2D shooter similar to something like Intrusion 2currently at the stage where i have a character and am trying to make it so he always aims at the mouses location. It works perfectly if the character faces right, but if i turn him around the arm faces is the exact opposite direction of the mouse.

Ill upload a picture to show what i mean:
2209227--146836--Error.png . I am

Any help is appreciated.
Thanks, Sam

Try something like this:

using UnityEngine;
using System.Collections;

public class AimMouse : MonoBehaviour
{   void Update()
    {
        Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 dir = mousePos - (Vector2)transform.position;

        transform.eulerAngles = new Vector3(0, 0, Mathf.Rad2Deg * Mathf.Atan2(dir.y, dir.x));
    }
}

Hi, thanks for the reply :slight_smile:

I have tried what you have there and it still does the same as my code. It only looks at the mouse if the character is facing to the right, if i move the character left so he is facing the left hand side the arm looks at the exact opposite direction to where the mouse is. I assume its something to do with how unity mirrors sprites but i dont know how to fix it.

Thanks again, Sam

Is the players arm animated separately to the rest of the body? It looks like it is mirroring the body on one axis & the arm or gun on another axis

Yeah the body is part of one animation prefab and then the arm is separate from the rest to it can work on a swivel. At least that was what i was hoping for.

Then I’d suggest checking the swivel because the arm is moving but it looks like on the wrong axis (the gun is upside down )

Is there a way to make it so that i can change the rotation of an object if the character is facing left?

Does the character ever put the gun down? If not you could have just the one sprite & flip it or if you didn’t want it to look like he was switching arms to shoot create 2 sprites & just show the oNe that matches the direction he is shooting.

Hmm ok, thanks guys i’ll ill try making the two sprites and that should work.

Thanks, Sam

Good luck, & this should let you progress & test more stuff in your game. You can always go back later & try to get it working again with the swivel if you aren’t satisfied with how it looks.

I’m guessing you are applying scale -1 to the x? This will reverse the collider and all the transform too.

We have a feature coming out soon that allows you to visually flip the sprite but not everything else.

I guess, for now you need to account for this for your feature to work.

A little bit late, but… If the arm is parented to the body and the body is flipped using a negative scale, then this should solve your problem:

using UnityEngine;
using System.Collections;

public class AimMouse : MonoBehaviour
{   void Update()
    {
        Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 dir = mousePos - (Vector2)transform.position;

    transform.eulerAngles = new Vector3(0, 0, Mathf.Rad2Deg*Mathf.Atan2(dir.y, dir.x) * Mathf.Sign(transform.parent.localScale.y));
    }
}

Hi, is this feature you mentioned included in 5.3 ?