1

I am making a Top-Down 2D shooting game and i did spot a trouble. The point is, player gun has weird rotation whenever the player rotate.

I made my Player face mouse position. Player gun is not in the center of sprite. The Gun is a prefab in PlayerHand and PlayerHand is a Child of Player. I tried a lot of things and yet still i cant find a solution.

public class HandHolder : MonoBehaviour 
{

    [SerializeField] Gun gun;
    [SerializeField] float offsetX;
    [SerializeField] float offsetY = 0.01f;
    Gun playerGun;

    void Awake () 
    {
        playerGun = Instantiate(gun,transform.localPosition,transform.localRotation) as Gun;
    }

    // Update is called once per frame
    void Update () 
    {
        playerGun.transform.position = new Vector3(transform.position.x + offsetX,transform.position.y + offsetY);
        playerGun.transform.rotation = transform.rotation;
        playerGun.Shooting();
    }
}

    void Update() 
    {

        if (!isLocalPlayer)
            return;
        Vector3 position = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized * Time.deltaTime * 20f;
        transform.position += position;
        FaceMouse();
    }

    public void FaceMouse() 
    {
        Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        mousePosition.Normalize();
        float rotation_z = Mathf.Atan2(mousePosition.y, mousePosition.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0f, 0f, rotation_z);
    }

Here are the screenshoots. My Player already has a gun in texture. but it is only texture. I want the Gun prefab to be exactly on the place of Player sprite gun, whenever i rotate.

The issue on screen

derHugo
  • 83,094
  • 9
  • 75
  • 115
Kiket95
  • 73
  • 10
  • The pivot of the player's gun should be the pivot which the player is rotating around – Ryolu Oct 17 '18 at 01:14
  • Could you explain me how can i set same pivot for two different sprites? – Kiket95 Oct 17 '18 at 01:20
  • My mistake, they do have the same pivot on Instantiate. It seems like your offset might be causing your issue. Have you checked in which order are these 2 `Update()`s called? – Ryolu Oct 17 '18 at 01:33
  • Problem solved, i have no idea how, but it works now. I removed offsets. I just added them to check if I can move gun on point. But last time while I havent got them, there was still a problem. No idea how it helped now. Anyway, thank you ! :) – Kiket95 Oct 17 '18 at 01:40
  • If the gun anyway is somehow a child of the player why do you have to set it's rotation anyway? It should already be done by rotating the entire player, right? – derHugo Oct 17 '18 at 04:58

1 Answers1

1

add an empty to your player, name it gunTransform. tag it GunTransform make sure the forward axis(blue) is facing the players forward direction.

Class Level variable -

Transform guntransform;

in Awake():

guntransform=this.GameObject.FindObjectWithTag("GunTransform").getComponent<Transform>();

then instead of

playerGun = Instantiate(gun, transform.localPosition, transform.localRotation) as Gun;

call

playerGun = Instantiate(gun, guntransform.position, guntransform.rotation) as Gun;
derHugo
  • 83,094
  • 9
  • 75
  • 115
Technivorous
  • 1,682
  • 2
  • 16
  • 22