|
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?
(comments are locked)
|
|
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. 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)
|
|
For the keyboard aiming (I haven't done much with mouse aiming..) you can use something like the following:
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
(comments are locked)
|
