How To Make it Aim in Right Direction?

Hi! I try to make a shooting system in my game and I can’t figure out how to make my character aim in right direction.

As you can see in the video above a red line, which represents the aiming direction, doesn’t match a mouse position. To achieve the result I just rotated the gun around Z axis using such expression:

gun.transform.rotation = Quaternion.Euler(0, 0, touchControlsManager.TouchPosition().y);

But It doesn’t work fine. Could somebody help me to calculate the aiming direction according a mouse position?

Your code is wrong. If you have your weapon in let’s say (0, 0) point, then if your touch position is (1, 1), then the result angle should be 45 angles relative to the ground. Then let’s say you move your mouse in (2, 2) point, angle should still be 45 angles, but in your case it would different, world positions have nothing to do with euler angles which is simple 360 angle system.
In your case you need something like:

transform.LookAt(mousePosition)
1 Like

Unfortunately such expression is not relevant for me because LooAt oriented to Z axis. But I started searching for LookAt for Vector2 and found that solution: