0
\$\begingroup\$

I have a 2D game and I'm working on head rotation with my newbie coding skills. My Player has child sprite object called DefaultPistol (which is the whole arm and pistol attached to it) and it has a Weapon script. The arm turns 360 (centered from the shoulder) in the scene.

What I need is for my player turn his Head wherever arm turns (the Head is also another child sprite object attached to Player like DefaultPistol) but not like rotating the head, I just simply want to mirror the head to face left or right.

I tried setting the head's x scale to 1 or -1, but it didn't work and I couldn't find what is the problem. (I also checked the inspector if I didn't attach anything, but everything is in order as far as I can see)

Diagram of head flip

Here is my Weapon. You can see where I tried to implement this in Flip. How can I fix this?

public class Weapon : MonoBehaviour {

    public GameObject projectile;
    public Transform shotPoint;
    public float timeBetweenShots;
    private Animator cameraAnim;

    private float shotTime;
    private bool facingRight;
    public GameObject head;


    private void Start()
    {
        cameraAnim = Camera.main.GetComponent<Animator>();
    }

    private void Update()
    {   //this code paragraph here is calculating the shotPoint player rotation
        Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        Quaternion rotation = Quaternion.AngleAxis(angle + 90, Vector3.forward);
        transform.rotation = rotation;

        if (Input.GetMouseButton(0))
        {
            if (Time.time >= shotTime)
            {
                Instantiate(projectile, shotPoint.position, transform.rotation);
                cameraAnim.SetTrigger("shake");
                shotTime = Time.time + timeBetweenShots;
            }

        }

    }

    private void Flip(float horizontal)
    {
        if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
        {
            facingRight = !facingRight;
            Vector3 theScale = transform.localScale;
            head.transform.localScale = new Vector3();
            theScale.x *= -1;

            transform.localScale = theScale;
        }

    }

}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ It looks like you are not calling the Flip method anywhere. It also seems to turn the localScale of the head to a new Vecotr3(), which would be a 0:0:0 vector, which would make the head disappear. It then mirrors the scale of the object the script is on, not the head. \$\endgroup\$
    – Philipp
    Apr 14, 2021 at 14:32
  • \$\begingroup\$ Hmm, I don't know where I can call the Flip method. Also, can you be more spesific for solution in view of the fact that I am newbie on coding, please :) \$\endgroup\$ Apr 14, 2021 at 14:44
  • \$\begingroup\$ Well, when you don't call the method, then it can't do anything. \$\endgroup\$
    – Philipp
    Apr 14, 2021 at 14:51

1 Answer 1

2
\$\begingroup\$

You should use SpriteRenderer's FlipX property.You dont need any kind of Vector scale operations just simply use SpriteRenderer's builtin property

Example

public class CharacterHeadFlipScript : MonoBehaviour
{
    public SpriteRenderer characterHeadSpriteRenderer; // just drag and drop the sprite renderer of your character's head via editor
    
    void Update()
    {
        /*
         * First get the current mouse world position
         */
        Vector2 mouseScreenPosition = Input.mousePosition; // gets the current position of the mouse in screen space coordinates (Pixels)
        Vector2 mouseWorldPosition = Camera.main.ScreenToWorldPoint(mouseScreenPosition); //Converts screen space coordinate to world coordinates

        /*
         * Get the character's body position
         * Im asuming you add this script to the character you want to flip its head
         */
        Vector2 characterPosition = this.transform.position;

        /*
         * Check if mouse is on the right side of your character or on the left side
         * I will assume that your character's default head direction is the right side
         */
        if(mouseWorldPosition.x > characterPosition.x) // mouse on the right side of the character
        {
            /*
             * This means character should look to default direction(right side) so we should disable FlipX of the SpriteRenderer
             */
            characterHeadSpriteRenderer.flipX = false;
        }
        else // mouse on the left side of the character
        {
            /*
             * This means character should look to opposite direction(left side)
             */
            characterHeadSpriteRenderer.flipX = true;
        }

    }

}
\$\endgroup\$
2
  • \$\begingroup\$ Thank you, I dealt with the situation. Now Player turns his head wherever his arm position. \$\endgroup\$ Apr 14, 2021 at 19:15
  • \$\begingroup\$ Actually now there is a little problem occured, my player is also moving. When he goes to right side, code working very good, but when he runs left side, his weapon_arm and head are become opposite. \$\endgroup\$ Apr 14, 2021 at 19:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .