EulerAngles aberation with perspective camera in "2D" game.

Hello everyone !

I hope this question where never ask, because I can’t find any answere.

I made a shmup game in 2D with a perspective camera. I’m trying to do an “Aiming Shot” bonus to shoot where you click.

The “Canon” must rotate around the Player, by Following the mouse. So I create this little script :

public void FixedUpdate()
    {
      
        if (playerStatistic.isAimingActive == false)
        {
            isAiming = false;
            return;
        }
        if (playerStatistic.isAimingActive == true)
        {

            //rotation de la souris autour du joueur.
            Vector3 mouseposition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 50); // avec la camera en perspective, il faut préciser la profondeur, sans quoi les coordonnées sont faussées.
            var newMouseposition = Camera.main.ScreenToWorldPoint(mouseposition);
            Ray ray = new Ray(transform.position, newMouseposition);
            Debug.DrawRay(transform.localPosition, newMouseposition, Color.white);
            Debug.DrawRay(transform.position, newMouseposition, Color.yellow);
            Debug.DrawLine(transform.position, newMouseposition, Color.blue);
            Debug.DrawLine(transform.localPosition, -newMouseposition, Color.red);
            Quaternion rotation = Quaternion.LookRotation(transform.position, -newMouseposition);
            transform.rotation = rotation;
            float angle = transform.localEulerAngles.z - 90;
            transform.eulerAngles = new Vector3(0, 0, angle);
        }
    }

I Solved many of problèmes with the use of RayCast and the ScreenToWorldPoint. But i have an “aberation”. When the player is far of the mouse click, the shoot is … wrong. But when the click is near the player, the precision is good. I Don’t understand because i can’t find the good raycast or the good solution … and the DrawLine debug tool is wrong too :frowning:

For more précisions, I use this camera settings :

And the player is Instantiate at a Z distance of 50. The player move in the scene, the camera can’t move.

Any helps or tips ?

I’m uploading a WebGL version of the game, I can give you the link later!
You can “test” the bug by playing my game :stuck_out_tongue:

Thank You very much !

Sorry for my English, i’m not used to speak it or write it …

You can see my website here : http://japandoudou.com
You can test the game here : http://unpangolin.free.fr/spacepioupiou/OnlineBuild0.0.9.2/index.html

More precision : the precision decrease with the distance between the player and the center of the screen, and not the distance between the click and the player …

Once you start dealing with a perspective camera you have to start treating these things a lot differently. Here’s what I found from a quick search:

There are also serious problems with your code, most notably here:

Quaternion rotation = Quaternion.LookRotation(transform.position, -newMouseposition);
transform.rotation = rotation;
float angle = transform.localEulerAngles.z - 90;
transform.eulerAngles = new Vector3(0, 0, angle);

I’m not sure what the intention is because transform.rotation and transform.eulerAngles are properties that refer to the same underlying value. I’d also avoid assuming a transform’s euler angles will be represented in any given fashion.

If I had to guess you want the angle between those two points and to assign a rotation purely represented on the Z-axis. If that’s the case it’s trivial to calculate it for yourself:

Vector3 delta = newMouseposition - transform.position;
float angle = Math.Atan2(delta.y, delta.x) * Mathf.Rad2Deg;
transform.eulerAngles = new Vector3(0, 0, angle);
1 Like

It works absolutely fine, i’m impressed… I knew the solution was easy , but not so easy. I must read my math book again :frowning:

Thank you very much … I have a lot of things to learn here !