• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Persona · May 02, 2011 at 10:08 PM · 2dcursormouselookaim

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);
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NinjaSquirrel · May 08, 2011 at 03:35 PM 0
Share

That's what I use.

avatar image Persona · May 09, 2011 at 01:35 PM 0
Share

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?

avatar image hypnoslave · May 10, 2011 at 07:56 PM 0
Share

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...

avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Reticle orbits around player depending on right stick direction? 0 Answers

Cursor sprite edge rotate towards mouse position relative to player 0 Answers

my gun wont follow my mouse 0 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

how can make Ik targets following cursor?,,how can i make IK targets follow the cursor? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges