- Home /
Projectiles in 2.5D game
Hi there I am making a 2.5D game so basically a 2d top down game but I have taken my camera and angled it -30 on the x rotation. this way I can put in 3d elements as well and I really like the style.
However I have run into a probelm with projectiles.
In a normal top down game you can shoot a projectile at your mouse with this code:
Vector3 mouse = Input.mousePosition;
mouse = Camera.main.ScreenToWorldPoint(mouse);
Vector2 direction = new Vector2(
mouse.x - transform.position.x,
mouse.y - transform.position.y
);
Debug.DrawLine(transform.position, mouse, Color.cyan);
Well this draws a debug line but you get my drift. this will give you the correct direction to launch your projectile.
However I am unsure as to how to go about correcting for the -30 degree angle on my camera.
any help would be appreciated.
regards.
Still cant find how to compensate for the rotation of the camera.
Ways to reproduce.
Open a scene and make a script with the following code:
//debug
LineRenderer line;
Vector3 mouse = Input.mousePosition;
mouse = Camera.main.ScreenToWorldPoint(mouse);
Vector3 direction = new Vector3(
mouse.x - projectilePoint.transform.position.x,
mouse.y - projectilePoint.transform.position.y,
mouse.z = projectilePoint.transform.position.z
);
line.startColor = Color.cyan;
line.startWidth = 0.1f;
line.SetPosition(0, projectilePoint.transform.position);
line.SetPosition(1, direction);
set the camera to position 0, -5.5, 0 rotation -30, 0,0
I solved this issue while responding to another user I wrote up my answer here:
On a side note, in case someone has the same situation as me. If you then wish to rotate your sprite "bullet" correctly, do this:
// turn the projectile correctly
float angle = $$anonymous$$athf.Atan2(direction.y, direction.x) * $$anonymous$$athf.Rad2Deg;
projectile.transform.Rotate(0, 0, angle);