Home Game Development unity – Limiting shooting angle only works when facing right

unity – Limiting shooting angle only works when facing right

0
unity – Limiting shooting angle only works when facing right

[ad_1]

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);
        }
        else
        {
            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);
        Destroy(prefEffect);
    }   
}

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

[ad_2]