Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 timo0060 · May 08, 2012 at 02:22 PM · javascriptmouseshootingaimingsidescroller

Aiming with the mouse in a 2d shooter, and then shooting to the mouse

So I was wondering how I can use the mouse to aim in my 2d sidescroller and then shoot to where the cursor is. I've looked at some questions asked on this site that are like mine already, but so far none of them have helped. Any help is most appreciated. Thank you all in advance.

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
0

Answer by Berenger · May 08, 2012 at 02:45 PM

You can add a box collider in front of the camera then use Physics.Raycast and ScreenPointToRay. Or Plane.Raycast with a Plane( Vector3.back, Vector3.zero ).

Comment
Add comment · Show 1 · 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 timo0060 · May 08, 2012 at 05:42 PM 0
Share

Now how exactly would I put that into code? I looked into both, but they don't seem to have good examples of how to use it for ai$$anonymous$$g with the mouse and shooting.

avatar image
0

Answer by Piflik · May 08, 2012 at 05:56 PM

I have an invisible plane in my scene that is used to calculate the position of the mouse pointer in the 3D game world:

 private var layerMask  = 1 << 8;   //plane is on layer 8

 if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), mousPos, Mathf.Infinity, layerMask)){
     //lookAt mousPos.point
 }

(should be even easier in a top down 2D shooter)

For shooting:

 if(Input.GetKey(KeyCode.Mouse0)) {
     //shoot
 }
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 timo0060 · May 08, 2012 at 06:02 PM 0
Share

I don't exactly get what the plane and layermask are. I'm still pretty new with Unity. Can you explain it a bit more, or give me a link to more information about it?

avatar image Piflik · May 08, 2012 at 06:09 PM 0
Share

The layer $$anonymous$$ask is essentially a binary code to say which layers are visible to the ray. 1 << 8 is a 1, shifted left 8 times, so the result is 10000000. This means the 8th layer is visible, all others are not. 10100101 would be layer 8, 6, 3 and 1 are visible, 7, 5, 4 and 2 are invisible. (I omitted leading 0s. The complete code has 32 digits/bits)

The plane is a standard unity plane-object. I disabled the $$anonymous$$esh Renderer Component, so it isn't visible to the camera, and put it on layer 8.

avatar image timo0060 · May 08, 2012 at 06:26 PM 0
Share

Thank you for explaining that. I have 1 more question... maybe 2 more depending on if I encounter problems. Can you explain how to make it aim to where the cursor is. What I mean is, can you explain the look at code more? Sorry, but I'm very new to program$$anonymous$$g with unity.

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bullets follow mouse after shooting in Sidescroller 1 Answer

Sidescroller Bullets Follow Mouse After Being Shot 2 Answers

Trouble with aiming with the mouse in a 2D sidescroller 0 Answers

My Bullets aren't moving... 1 Answer

Code Converstion into javascript Help? 2 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