x


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);
more ▼

asked May 02 '11 at 10:08 PM

Persona gravatar image

Persona
246 74 85 96

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered May 08 '11 at 12:15 AM

hypnoslave gravatar image

hypnoslave
436 12 16 25

That's what I use.

May 08 '11 at 03:35 PM NinjaSquirrel

var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit)) { AimingTarget = Vector3(hit.point.x, hit.point.y, 0); } transform.LookAt(AimingTarget);

It seems to slightly change the rotation to fire into the camera for me. Can you show me a coding example?

May 09 '11 at 01:35 PM Persona

I can't at the moment -- try using Collider.Raycast instead -- 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...

May 10 '11 at 07:56 PM hypnoslave
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered May 05 '11 at 04:38 PM

Chris D gravatar image

Chris D
2.5k 5 7 25

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1041
x198
x162
x103

asked: May 02 '11 at 10:08 PM

Seen: 2192 times

Last Updated: May 02 '11 at 10:08 PM