Unity 5 2D Pointing gun with mouse help! (Solved)

Hello.
I’m trying to make 2D platformer run n shoot style game with Unity 5.1, but I’m stuck already :frowning:

When I’m pointing gun at right it works perfectly but when I move mouse to left side of player, the player flips as it should, and gun flashes on both right and left side of player like crazy. On right gun points to exact opposite direction of mouse, as if it’d try to point on both sides.

Another bug when moving mouse back and forth → mouse is on right side, but gun is on left side pointing exact opposite direction of mouse. This is ‘fixed’ by moving mouse back and forth again.

Gun is childed under empty GameObject, which has this script attached:

  using UnityEngine;
  using System.Collections;
  
  public class Gun : MonoBehaviour {
    
  Quaternion rotation;
  public Transform shoulder; //empty GameObject to place gun
  public Transform target; //this is mouse, works fine
  public Transform flipper; //empty GameObject to check if sprite should flip
  public bool DirectionRight = true;
  
     void Update ()
     {  
  
     transform.position = shoulder.position;
      
     if (target.position.x > flipper.position.x && DirectionRight == false) Flip();
  
  
     if (target.position.x < flipper.position.x && DirectionRight == true) Flip();
  
     if (DirectionRight == true && target.position.x > flipper.position.x)
     {
       rotation = Quaternion.LookRotation (target.transform.position - transform.position, transform.TransformDirection (Vector2.up));
       transform.rotation = new Quaternion (0, 0, rotation.z, rotation.w);
     }
  
     if (DirectionRight == false && target.position.x < flipper.position.x)
     {
       rotation = Quaternion.LookRotation (target.transform.position - transform.position, transform.TransformDirection (Vector2.down));  
       transform.rotation = new Quaternion (0, 0, rotation.z, rotation.w);
     }
  
  
     }
  
     void Flip()
     {
       DirectionRight = !DirectionRight;
  
       Vector2 tempScale = transform.localScale;
       tempScale.x *= -1;
       transform.localScale = tempScale;
     }
    
  }

I answered a similar question a while back. It might help with your problem;

Thanks for the link, however it still didnt work right.
Still, I got it working right :slight_smile: I even somehow fixed the “stuck on wrong side” problem.

My GameObjects:

Player
–playerBody
–playerHead
–Flipper <— the flipper is child of Player
–etc…

Gun <— the Gun is not child of Player, this is empty gameobject with the Gun script
–TheGun <— the actual gun sprite
----GunTip

using UnityEngine;
using System.Collections;

public class Gun : MonoBehaviour {
  
    Quaternion rotation;
    public Transform target; //mouse's location
    public Transform flipper; //checker if mouse is left/right side of player
    public Transform gunTip; //gun's tip
    public bool DirectionRight = true; //direction

    void Update ()
    {  
        //Place the gun in player object
        transform.position = flipper.position;

        //When to flip the gun
        if (target.position.x > flipper.position.x && DirectionRight == false)
        {
//            Debug.Log("Flip'd");
            Flip ();
        }

        if (target.position.x < flipper.position.x && DirectionRight == true)      
        {
//            Debug.Log("Flip'd");
            Flip ();
        }


        //Here is my fix for "gun is stuck on opposite side >:("
        if (gunTip.position.x < flipper.position.x && DirectionRight == true)
        {
//            Debug.Log("Something is wrong, doing Flip()");
            transform.Rotate(180,0,0); //Gotta flip gun 180 degrees on X.
            Flip ();
        }

        if (gunTip.position.x > flipper.position.x && DirectionRight == false)
        {
//            Debug.Log("Something is wrong, doing Flip()");
            transform.Rotate(180,0,0);//runs while w is held
            Flip ();
        }


        //I noticed that I don't have to have different IFs for both directions
        rotation = Quaternion.LookRotation (target.transform.position - transform.position, transform.TransformDirection (Vector2.up));
        transform.rotation = new Quaternion (0, 0, rotation.z, rotation.w);  
    }

    //Flipping the gun
    void Flip()
    {
        DirectionRight = !DirectionRight;

        Vector2 tempScale = transform.localScale;
        tempScale.y *= -1;
        transform.localScale = tempScale;
    }
  
}