0
\$\begingroup\$

I'm making a game where you shoot enemies in Unity, but I can't figure out how to stop the player from pointing the gun at himself.

I've tried detecting if the gun is on the left side of the player and if it's rotation on the z-axis is less than 0, but no luck

The gun pointing at the player

Here's the code for the gun:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GunController : MonoBehaviour
{
    public bool testbool;

    public Transform player;
    public CharacterController2D playercontroller;
    public float smoothness = 0.125f;
    public float winglength = 2;
    private Vector2 difference;
    bool facingright = false;

    public GameObject bullet;
    public int maxammo;
    private int ammo;
    private bool firing;
    public Vector3 bulletspawnpos;

    private SpriteRenderer sprite;
    public Texture2D normalGun;
    public Texture2D emptyGun;
    public GameObject emptyBottle;
    // Start is called before the first frame update
    void Start()
    {
        sprite = GetComponent<SpriteRenderer>();
        player = transform.parent.transform;
        ammo = maxammo;
    }

    // Update is called once per frame
    void Update()
    {

    }
    private void FixedUpdate()
    {
        firing = false;
        bool wasfacingright = facingright;
        if (Input.GetMouseButton(0) & ammo !>= 0 & !firing )
        {
            firing = true;
            Instantiate(bullet, transform.position+bulletspawnpos, transform.rotation);
        }
        if (player.transform.position.x - transform.position.x < 0)
        {
            facingright = false;
            sprite.flipY = false;
        }
        if (player.transform.position.x - transform.position.x > 0)
        {
            facingright = true;
            sprite.flipY = true;
        }        
        if (wasfacingright != facingright) { playercontroller.Flip(); }

        difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float rot = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
       
        Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition) - player.position;
        pos.z = 0;
        Vector3 desiredpos = player.position + (winglength * pos.normalized);
        Vector3 smoothpos = Vector3.Lerp(transform.position, desiredpos, smoothness);
        transform.position = smoothpos;
        transform.rotation = Quaternion.Euler(0, 0, rot);

    }
}

Before you point out that the ammo doesn't go down, just know I didn't add that for testing purposes.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ I am not sure what you mean with "the gun pointing at yourself". Can you perhaps add a couple screenshots which illustrate the problem? \$\endgroup\$
    – Philipp
    Feb 14, 2021 at 16:39
  • \$\begingroup\$ Why do you ammo!>=0, shouldn't it be ammo>0? \$\endgroup\$
    – Niels
    Feb 15, 2021 at 8:22

1 Answer 1

-1
\$\begingroup\$

I'm pretty sure that it is pointing to the camera, and not the player. It happened to me.

\$\endgroup\$
2

You must log in to answer this question.

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