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

Discussion in '2D' started by JapanDoudou_fr, Jul 11, 2019.

1. ### JapanDoudou_fr

Joined:
Jul 11, 2019
Posts:
7
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 :

Code (CSharp):
1. public void FixedUpdate()
2.     {
3.
4.         if (playerStatistic.isAimingActive == false)
5.         {
6.             isAiming = false;
7.             return;
8.         }
9.         if (playerStatistic.isAimingActive == true)
10.         {
11.
12.             //rotation de la souris autour du joueur.
13.             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.
14.             var newMouseposition = Camera.main.ScreenToWorldPoint(mouseposition);
15.             Ray ray = new Ray(transform.position, newMouseposition);
16.             Debug.DrawRay(transform.localPosition, newMouseposition, Color.white);
17.             Debug.DrawRay(transform.position, newMouseposition, Color.yellow);
18.             Debug.DrawLine(transform.position, newMouseposition, Color.blue);
19.             Debug.DrawLine(transform.localPosition, -newMouseposition, Color.red);
20.             Quaternion rotation = Quaternion.LookRotation(transform.position, -newMouseposition);
21.             transform.rotation = rotation;
22.             float angle = transform.localEulerAngles.z - 90;
23.             transform.eulerAngles = new Vector3(0, 0, angle);
24.         }
25.     }
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

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 ?

You can "test" the bug by playing my game

Thank You very much !

Sorry for my English, i'm not used to speak it or write it ...

2. ### JapanDoudou_fr

Joined:
Jul 11, 2019
Posts:
7
Last edited: Jul 11, 2019
3. ### ashapcott

Joined:
Apr 4, 2019
Posts:
27
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:

Code (CSharp):
1. Quaternion rotation = Quaternion.LookRotation(transform.position, -newMouseposition);
2. transform.rotation = rotation;
3. float angle = transform.localEulerAngles.z - 90;
4. 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:

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

JapanDoudou_fr likes this.
4. ### JapanDoudou_fr

Joined:
Jul 11, 2019
Posts:
7
It works absolutely fine, i'm impressed… I knew the solution was easy , but not so easy. I must read my math book again

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