Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 CubePhysics · Jul 18, 2015 at 10:53 PM · gameobjectinputdetection

how to detect a mouse click that happened but wasnt on the gameobject?

Is there a way to detect a mouse click input and do something but only if the click was somewhere other than the gameobject? so its checking if the mouse was clicked but the gameobject was clicked? then do something. For example shoot if mouse is clicked anywhere but dont shoot if mouse is clicked on gameobject.

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 Positive7 · Jul 18, 2015 at 11:10 PM

using UnityEngine; using System.Collections;

public class Tester : MonoBehaviour { public bool shotEnabled;

 void Update () {
 
     RaycastHit hit;
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if (Physics.Raycast (ray, out hit, 100f)) {
         if (Input.GetMouseButtonDown (0)) {
             if (hit.transform.name == "Cube") {
                 shotEnabled = false;
             } else {
                 shotEnabled = true;
             }
         }
     }
 }

}

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 CubePhysics · Jul 20, 2015 at 04:41 PM 0
Share

this doesnt seem to work

avatar image
0

Answer by PDay · Jul 20, 2015 at 05:03 PM

This should do as you want. It works on a simple cube object with collider. You'll want to change the two Debug.Log lines to a function you want to call in the instances where it is hit or missed:

public class MissScript : MonoBehaviour {

 void Update () {
     if (Input.GetMouseButtonDown(0))
     {
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     RaycastHit hit;
     
     if (Physics.Raycast(ray, out hit) && this.gameObject.Equals(hit.transform.gameObject)) Debug.Log ("Hit");
     else Debug.Log ("Miss");
     }
 }

}

Comment
Add comment · Show 4 · 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 CubePhysics · Jul 24, 2015 at 12:46 PM 0
Share

This works for a 3D gameobject, how do i get it to work for a 2D box collider because when i click on the 2d box collider i get a miss

avatar image ScienceIsAwesome · Jul 24, 2015 at 01:18 PM 0
Share

You need Physics2D for a 2D collider

avatar image CubePhysics · Jul 24, 2015 at 02:26 PM 0
Share

I replaced Physics with Physics2D in the if statement but i just get an error. How do i change the script to match a 2D collider

avatar image Positive7 · Jul 24, 2015 at 08:38 PM 1
Share
 void Update () {
         Vector3 screenPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
         
         if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             if(Physics2D.Raycast(screenPos, Vector2.zero) && hit.collider.name == "Cube"){
                 Debug.Log ("Hit");
             }else{
                 Debug.Log ("$$anonymous$$iss");
             }
             
         }
     }

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

25 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detect if running on mobile vs desktop 2 Answers

How to get different Animations when different button combos are pressed 2 Answers

Collision Checking 1 Answer

How to mimic Unity's Components based System 1 Answer

Adding an offset to instantiated object based on mouse distance from player 1 Answer


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