• 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 Ronin6 · Jan 14, 2013 at 10:46 PM · shootingjoystickonmousedownalternativeonmouse

Alternative to OnMouseDown()

I am making a shooting game where the mouse moves the aiming crosshair and the targets are using OnMouseDown() to detect when they are hit. I coded the crosshair movement for joysticks but of course OnMouseDown() will not work when using a joystick. Does anyone have any ideas that can help me get the hit detection working? I do not think raycast will work because I am using a perspective camera and the crosshair does not stay at the center of the camera, but maybe I am wrong with this assumption. (Similar games would be Lethal Enforcers, Virtua Cop, House of the Dead)

alt text

Screenshot: The white circle is the cross hair and the white boxes are the targets. The ray can hit the boxes near the center but not the boxes further away. Probably due to perspective.

debugpic.png (11.1 kB)
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 Lovrenc · Jan 14, 2013 at 10:52 PM

This is the solution.

Comment
Add comment · Show 11 · 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 Ronin6 · Jan 14, 2013 at 11:40 PM 0
Share

I dont know if this will work because in my game the crosshair moves and can be anywhere on the screen.

Its not like a modern first person shooter where the camera moves and the crosshair stays centered. Its an on-rails shooter like Virtua Cop where the camera automatically moves through the level and the player moves the crosshair around the screen.

avatar image Lovrenc · Jan 14, 2013 at 11:45 PM 0
Share

Then:

Camera.ScreenPointToRay

avatar image Ronin6 · Jan 14, 2013 at 11:46 PM 0
Share

This may work. Im going to give it a shot.

avatar image Ronin6 · Jan 15, 2013 at 12:18 AM 0
Share

I tried Camera.ScreenPointToRay but it only reaches its targets when firing at the center of the camera. I believe firing further from the center does not work because it is distorted by perspective and the ray can not reach the targets. It must have something to do with the z position of Camera.ScreenPointToRay(new Vector3(x,y,z))

Thanks for trying though. I been attacking this problem for days now.

avatar image Lovrenc · Jan 15, 2013 at 12:21 AM 0
Share

Post a pic of what you are doing.

Show more comments
avatar image
0

Answer by petex · Jan 30, 2013 at 11:56 AM

i use this for get component on target and use it function when i press joystick button 5,u may set condition in getkey to other.Destroy() ,hope this help.

 function Update () {
 
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit, 100)) {
         if ( hit.collider.gameObject.tag == "tag" ){
         var other: scriptname;
         other = hit.collider.gameObject.GetComponent("scriptname");
         if(other == "null"){
             Debug.Log("null script");
         }
         if(other != "null"){
             if(Input.GetKeyDown(KeyCode.JoystickButton5)){
             Debug.Log("ok script");
              other.Fn_on_target_script();
              }
          }
         }
       }
       
       
 }
 
 *******************
 on target script
 
 function Destroy(){
   Destroy (gameObject);
 }
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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 People are following this question.

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

Related Questions

Increase mouseclick area size for OnMouseDown() - For Mouse Shooting Game (Open to more ideas) 2 Answers

How can I call OnMouse functions on a specific collider? 2 Answers

GUI Joystick 1 Answer

How do I change this code so that i can press on the joystick more than once to shoot? 1 Answer

How to shoot using the right mobile Joystick? 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