[RaycastHit] Targeting the opponent with the mouse

Hello, I’m trying to target the opponent with a mouse.

I have this code that should work.

But after the character turns, the shooting is deflected.
If I rotate the characters, the shooting is directed in a different direction from where the cursor lies.

I went through some topics on the Internet, it did not help me.

Can I ask for less help?
Where am I making a mistake?

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                Rigidbody clone;
                clone = Instantiate(projectile, _weapons.transform.position, _weapons.transform.rotation) as Rigidbody;
                clone.velocity = transform.TransformDirection(hit.point - _weapons.transform.position);
                Debug.DrawLine(ray.origin, hit.point);
            }

Thank you very much for your help.

transform.TransformDirection translates from local to world space this will be where your issue lies. Just do (hit.point - _weapons.transform.position).normalized;

Unfortunately, it does not work.
Everything works correctly if the velocity defined as follows.

clone.velocity = (hit.point - _weapons.transform.position) * 10;

Now it works properly.

I’d still like to shoot at sides, up to 30 degrees.

The gun can only shoot + - 30 degrees, and the same thing I would like to do for the projectile.

Can someone direct me or help me?

Currently, my features look like this.

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, Mathf.Infinity))
            {
                rotationX += Input.GetAxis("Mouse X") * sensitivityX;
                rotationX = Mathf.Clamp(rotationX, -30, 30);
                rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                rotationY = Mathf.Clamp(rotationY, -30, 30);

                _weaponsSlot.transform.localEulerAngles = new Vector3(-rotationY, +rotationX, _weaponsSlot.transform.localEulerAngles.z);

                if (Input.GetMouseButton(0))
                {
                    Rigidbody clone;
                    clone = Instantiate(projectile, _weapons.transform.position, _weapons.transform.rotation) as Rigidbody;
                    clone.velocity = (hit.point - _weapons.transform.position) * 10;
                    Debug.DrawLine(ray.origin, hit.point);
                    Debug.Log(hit.ToString() + "\n" + ray.ToString());
                }
            }

Sorry for late response never got notified that you responded.

To clarify you want the bullet to fire in a 30 degree cone around the target? So if you fired a few shots they would fire at different angles be still be towards that target?

If thats so you can do something like:

Public float fireRadius = 0.3f;

Vector3 targetPos = hit.point;
//Calculating a random direction
Vector3 variedOffset = new(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), 0.0f);
variedOffset.normalise();
//Scale it within our radiu
variedOffset *= Random.Range(0.0f, fireRadius);

//Offset the target relative to our rotation
targetPos += _weapons.transform.right * variedOffset.x;
targetPos += _weapons.transform.up * variedOffset.y;

Just use targetPos over hit.point for the rest and it should be good.