- Home /
2D shooter plataform aim,tutorial please?
Hi, i´m kind newbie in script.I following a mario clone tutorial,but i want a shooter,and you know, with the gun following the mouse. i searched alot of things about this,and i found but , the only one that worked ( i just copy and paste):
#pragma strict
var mouse_pos : Vector3;
var target : Transform;
var object_pos : Vector3;
var angle : float;
function Update ()
{
mouse_pos = Input.mousePosition;
mouse_pos.z = -20;
object_pos = Camera.main.WorldToScreenPoint(target.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
}
Aim = new Vector3((target.transform.position.x - transform.position.x), target.transform.position.y - transform.position.y, 0);
transform.rotation = Quaternion.FromToRotation (Vector3(0, 5, 0), Aim);
,didnt work very well(the object(player ) that should rotate following the mouse,follows, but it turn 180 in Y too,and as is a plane(2d)the texture face the other side ,so just becomes invisible,but i know that still follows the x,y of the mouse).
As i kind dont understand this code ,i wish someone could please help,with a explanation for noobs about the 2d aim system,or just solve the problem that i got with the current code its gonna make me wonderfully grateful as well.
Answer by boni · Aug 03, 2013 at 06:23 AM
As you'll know, Unity uses 3D coordinates, however the screen (the thing the camera displays) is 2D, as well as your mouse-coordinates.
The first 2 Lines in the Update
mouse_pos = Input.mousePosition;
mouse_pos.z = -20;
obtain the position of the mouse cursor on the screen and actually moves the position 20 "in front" of the screen. Next, the targeted objects position is obtained and transformed into screen-space:
object_pos = Camera.main.WorldToScreenPoint(target.position);
What this does, is that it takes the position of the object target in the game engine (the usual x/y/z coordinate) and transforms it to the coordinate system of the screen. So if you see an object ingame, and it's, let's say, 100 pixels from the bottom and from the left on your screen, this operation actually gives you that: a Vector3 that says it's (100,100,Z).
Now here comes the weird part with the Z-coordinate. As we can read in the WorldToScreenPoint-Documentation, "the z position is in world units from the camera". I assume the guy who wrote this script had his camera 20 units away from his objects, that's why he sets the mouse_pos.z to -20 above. Personally I'd just set all the Z positions to 0, to get 2D-Coordinates no matter where the camera is.
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
This part is simple, straight forward math. ;) He takes the X and Y distance of the mouse and the object on the screen, and calculates the angle. Just imagine a right angle triangle with the catheti beeing the x and y distance. In case you don't know trigonometry well, just know that tan(angle) = delta_y/delta_x, and therefore the angle = atan(y/x), which is exactly what MathF.Atan2(y,x) does. :)
transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
Quaternions represent a rotation. Quaternion.Euler returns the rotation around the X/Y/Z axis defined by a Vector3. If the Vector3 is (0,0,45), that means it'll be rotated by 45° around the z axis. Therefore this line rotates the object to look towards the target-object, since that's what "angle" represents.
I'm not exactly sure what the Aim-Line at the bottom is supposed to be, since it does nothing except transform.rotation = Quaternion.FromToRotation(Vector3.up*5, Vector3.zero). When is that thing called?
What I'm presonally missing in this script, is a section where he checks wether the mouse cursor is to the left or to the right of the object, since you'll have to mirror the gun if the mouse cursor is to the left, or it'll stand on its head. :)
Thanks ,that´s cleared my $$anonymous$$d.
First ,about the 2 last lines,that shouldn't be there(i just paste alot of codes that i found,just to figure it out,i forgot to erase that ),my fault.
And i cant put this code to work right,just one time it worked and than stopped,maybe is my scene configurtion or i´m placing the script in the wrong place,but the thing is that the target turn around,and turn its other face to the camera when start the "game",i even try to solve rotating everything as well ,but in this way the target follows the mouse inversely.
Ah, if I understand correctly the Y-axis is mirrored/inversed for the angle, right? I suppose this is because the Screen-Space has its root (0/0) at the bottom left, and positive numbers go upwards, but you're using the top-left with positive numbers going down as the root?
If it's the case, a simple Screen.height-y should fix it. Another idea, if the angle is just behaving really weird, is trying to switch the parameters for atan. Atan2(x,y) ins$$anonymous$$d of y,x.
If one of these doesn't fix it.. then I fear i don't fully understand the problem. :)
Thanks for sharing this it really helped alot :) Thumps Up :)
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Problem with gun rotation after flipping character (2D) 1 Answer
Shooting around defense position without contact 1 Answer
SideScrolling Mouse Pointer Aim - Help 0 Answers
Aiming a shot in Unity 2d? 0 Answers