- Home /
2D Cursor Aiming
I'm making a game in a two dimension format and needed help in setting up an aiming system similar to Pocket Tanks: http://www.youtube.com/watch?v=_HiVhsQV0Ow&feature=related
In short you can use the mouse or keyboard to adjust the angle of fire. I can do it in a 3d game using the following code, but it doesn't work in a 2D format. Can someone point me in the right direction?
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
AimingTarget = hit.point;
}
transform.LookAt(AimingTarget);
Answer by hypnoslave · May 08, 2011 at 12:15 AM
no problem man!
for mouse aiming, you can use a very similar system to what you're using for 3d, only make a large plane either in the background or foreground that you can use to raycast to using Collider.Raycast to get the mouse pointer position. change the depth axis to the level that your gameplay is on, of course.. so your target position would be made by doing something like: Vector3(hit.point.x, hit.point.y, 0);
then, simply have your character look at that point.
var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit)) { Ai$$anonymous$$gTarget = Vector3(hit.point.x, hit.point.y, 0); } transform.LookAt(Ai$$anonymous$$gTarget);
It seems to slightly change the rotation to fire into the camera for me. Can you show me a coding example?
I can't at the moment -- try using Collider.Raycast ins$$anonymous$$d -- that's a raycast that you can use against JUST that one collider in the scene. create a variable of type collider, plug in the collider into the script in the inspector, and call myColliderObject.Raycast
that will, at least, ensure that there are no problems with the ray hitting the wrong point.... which i think is your problem...
Answer by Chris D · May 05, 2011 at 04:38 PM
For the keyboard aiming (I haven't done much with mouse aiming..) you can use something like the following:
var angleAim : float = 0; //starting angle var angleSpeed : float; //how fast you want the angle to change
//using your vert. axis but this can be changed to Input.GetButton if (Input.GetAxis("Vertical")) angleAim += angleSpeed * Input.GetAxis("Vertical")*Time.deltaTime;
and then the same, essentially, for your power gauge.
For using the mouse look, the suggestion I have is make an invisible plane along the axis you're using (e.g. if you're locking all the gameObjects to z=0, make the plane along z=0) and then get the intersection from your raycast. From there, you can use some vector algebra to get your aim to follow. Sorry I don't have code ready for this part :(
EDIT: check out some other 2D Mouse Aim questions on here, too; there seem to be quite a few. One that looks promising is here: http://answers.unity3d.com/questions/4464/object-rotates-toward-mouse-2d-top-gameplay/4466#4466
Your answer
Follow this Question
Related Questions
Is it possible to not allow the in-game cursor through certain sprites? 1 Answer
How to stop object from glitching when coming too close to mouse? 0 Answers
2D Animation does not start 1 Answer
SideScrolling Mouse Pointer Aim - Help 0 Answers
Instantiating a bullet object from a 2D object that looks at mouse cursor 4 Answers