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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in '2D' started by JoKe90, Aug 14, 2015.

  1. JoKe90

    JoKe90

    Joined:
    Jun 10, 2015
    Posts:
    15
    Hello.
    I'm trying to make 2D platformer run n shoot style game with Unity 5.1, but I'm stuck already :(

    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:

    Code (CSharp):
    1.   using UnityEngine;
    2.   using System.Collections;
    3.  
    4.   public class Gun : MonoBehaviour {
    5.    
    6.   Quaternion rotation;
    7.   public Transform shoulder; //empty GameObject to place gun
    8.   public Transform target; //this is mouse, works fine
    9.   public Transform flipper; //empty GameObject to check if sprite should flip
    10.   public bool DirectionRight = true;
    11.  
    12.      void Update ()
    13.      {  
    14.  
    15.      transform.position = shoulder.position;
    16.      
    17.      if (target.position.x > flipper.position.x && DirectionRight == false) Flip();
    18.  
    19.  
    20.      if (target.position.x < flipper.position.x && DirectionRight == true) Flip();
    21.  
    22.      if (DirectionRight == true && target.position.x > flipper.position.x)
    23.      {
    24.        rotation = Quaternion.LookRotation (target.transform.position - transform.position, transform.TransformDirection (Vector2.up));
    25.        transform.rotation = new Quaternion (0, 0, rotation.z, rotation.w);
    26.      }
    27.  
    28.      if (DirectionRight == false && target.position.x < flipper.position.x)
    29.      {
    30.        rotation = Quaternion.LookRotation (target.transform.position - transform.position, transform.TransformDirection (Vector2.down));  
    31.        transform.rotation = new Quaternion (0, 0, rotation.z, rotation.w);
    32.      }
    33.  
    34.  
    35.      }
    36.  
    37.      void Flip()
    38.      {
    39.        DirectionRight = !DirectionRight;
    40.  
    41.        Vector2 tempScale = transform.localScale;
    42.        tempScale.x *= -1;
    43.        transform.localScale = tempScale;
    44.      }
    45.    
    46.   }
    47.  
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
  3. JoKe90

    JoKe90

    Joined:
    Jun 10, 2015
    Posts:
    15
    Thanks for the link, however it still didnt work right.
    Still, I got it working right :) 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

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Gun : MonoBehaviour {
    5.  
    6.     Quaternion rotation;
    7.     public Transform target; //mouse's location
    8.     public Transform flipper; //checker if mouse is left/right side of player
    9.     public Transform gunTip; //gun's tip
    10.     public bool DirectionRight = true; //direction
    11.  
    12.     void Update ()
    13.     {  
    14.         //Place the gun in player object
    15.         transform.position = flipper.position;
    16.  
    17.         //When to flip the gun
    18.         if (target.position.x > flipper.position.x && DirectionRight == false)
    19.         {
    20. //            Debug.Log("Flip'd");
    21.             Flip ();
    22.         }
    23.  
    24.         if (target.position.x < flipper.position.x && DirectionRight == true)      
    25.         {
    26. //            Debug.Log("Flip'd");
    27.             Flip ();
    28.         }
    29.  
    30.  
    31.         //Here is my fix for "gun is stuck on opposite side >:("
    32.         if (gunTip.position.x < flipper.position.x && DirectionRight == true)
    33.         {
    34. //            Debug.Log("Something is wrong, doing Flip()");
    35.             transform.Rotate(180,0,0); //Gotta flip gun 180 degrees on X.
    36.             Flip ();
    37.         }
    38.  
    39.         if (gunTip.position.x > flipper.position.x && DirectionRight == false)
    40.         {
    41. //            Debug.Log("Something is wrong, doing Flip()");
    42.             transform.Rotate(180,0,0);//runs while w is held
    43.             Flip ();
    44.         }
    45.  
    46.  
    47.         //I noticed that I don't have to have different IFs for both directions
    48.         rotation = Quaternion.LookRotation (target.transform.position - transform.position, transform.TransformDirection (Vector2.up));
    49.         transform.rotation = new Quaternion (0, 0, rotation.z, rotation.w);  
    50.     }
    51.  
    52.     //Flipping the gun
    53.     void Flip()
    54.     {
    55.         DirectionRight = !DirectionRight;
    56.  
    57.         Vector2 tempScale = transform.localScale;
    58.         tempScale.y *= -1;
    59.         transform.localScale = tempScale;
    60.     }
    61.  
    62. }
    63.