I have a problem with the shooting being limited when the player is turned to the left side. When I set a range from -145 to 145, on the contrary, it does not shoot into this range, but shoots from this range, i.e. -156, -178, 147, 167. I also have a restriction on the right side when the player is turned, everything works fine there.

enter image description hereenter image description hereenter image description here

    public class Weapon : MonoBehaviour
    [SerializeField] private float _offset;
    [SerializeField] private GameObject _prefabBullet;
    [SerializeField] private Transform _shootPoint;
    [SerializeField] private GameObject _effect;
    [SerializeField] private float _rightRotation;
    [SerializeField] private float _leftRotation;

    public IEnumerator Shoot()
        Vector3 diference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float rotateZ = Mathf.Atan2(diference.y, diference.x) * Mathf.Rad2Deg;

        if (Camera.main.ScreenToWorldPoint(Input.mousePosition).x < transform.position.x)
            //transform.rotation = Quaternion.Euler(new Vector3(180f, 0f, -rotateZ));
            rotateZ = Mathf.Clamp(rotateZ, -_leftRotation, _leftRotation);  // Ограничиваем угол влево
            transform.rotation = Quaternion.Euler(new Vector3(180f, 0f, -rotateZ));
            Instantiate(_prefabBullet, _shootPoint.position, _shootPoint.rotation);
            Debug.Log("Left" + rotateZ);
            rotateZ = Mathf.Clamp(rotateZ, -_rightRotation, _rightRotation);  // Ограничиваем угол вправо
            transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, rotateZ));
            Instantiate(_prefabBullet, _shootPoint.position, _shootPoint.rotation);
            Debug.Log("Right" + rotateZ);

        var prefEffect = Instantiate(_effect, _shootPoint.position, _shootPoint.rotation);
        yield return new WaitForSeconds(1f);

I'm not good at English, so I translate most words through a translator. I apologize if something is not clear

  • \$\begingroup\$ Just to make sure I've understood, is this correct? You want your character to be able to shoot in a 290 degree arc, centered at the vertical. So 0-145 degrees left OR 0-145 degrees right, but not towards the floor. When you say "-156, -178, 147, 167", do you mean "It moves smoothly from -156 to -178 then jumps to 147 and moves smoothly to 167"? If so, that implies there's a problem on the right side too [but you say "everything works fine" there?]. I'm unclear exactly what behaviour you are seeing. \$\endgroup\$
    – Basic
    Commented Nov 5, 2023 at 0:55


You must log in to answer this question.

Browse other questions tagged .