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
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
Thank You very much !
Sorry for my English, i’m not used to speak it or write it …