Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 unity_sxKFJS36l-DdLQ · Jan 05, 2021 at 10:02 PM · clicktap

Trying to detect mouse click or tap on a gameobject, why would not it work?

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class GameController : MonoBehaviour { int score = 0; int force = 1000;

 // Move balloons
 void Update()
  {
     //Calls bounce method each time button is clicked on a balloon
     if (Input.GetMouseButtonDown(0))
     {

         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hit) && hit.transform.CompareTag("Balloon"))
         {   
             Debug.Log("Touched a balloon");
             //hit.rigidbody.AddForce(ray.direction * hitForce);
         }
     }

     /*if tapped on a balloon, bounces it
     if ((Input.touchCount > 0) && (Input.GetTouch(0).phase == TouchPhase.Began))
     {
         Debug.Log("Touched screen");

         Ray raycast = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
         RaycastHit raycastHit;
         if (Physics.Raycast(raycast, out raycastHit))
         {
             if(raycastHit.collider != null)
              {
                 Debug.Log("Touched a balloon");
                 score = +1;
                 /*Rigidbody bl;
                 bl = raycastHit.rigidbody;
                 score++;
                 bl.AddForce(0, force, 0);
             }
         }
     }*/
 }

}

Comment
Add comment · Show 4
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 Llama_w_2Ls · Jan 05, 2021 at 10:16 PM 0
Share

Does the gameobject have a collider? Raycasts are fired using the Physics class, so you would need some sort of physics in there for it to detect a hit.

avatar image unity_sxKFJS36l-DdLQ Llama_w_2Ls · Jan 12, 2021 at 08:13 AM 0
Share

They have polygon collider each.

avatar image Llama_w_2Ls unity_sxKFJS36l-DdLQ · Jan 12, 2021 at 10:02 AM 0
Share

Are you sure the polygon collider matches up with the mesh? (Also, is your game 2D?)

avatar image unity_ek98vnTRplGj8Q · Jan 05, 2021 at 10:57 PM 0
Share

You can also use Debug.DrawRay to draw the ray in your scene view to make sure that it's actually hitting the object.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Connermarbut · Jan 05, 2021 at 11:37 PM

So testing it myself. It looked as if Unity wanted to know what the camera was. I made a private variable called cam, gave the camera the MainCamera tag. and then called it by tag.

  private Camera cam;
 
     void Start()
     {
         cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
     }
     
     void Update()
     {
         //Calls bounce method each time button is clicked on a balloon
         if (Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit) && hit.transform.CompareTag("Balloon"))
             {
                 Debug.Log("Touched a balloon");
                 //hit.rigidbody.AddForce(ray.direction * hitForce);
             }
         }

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

Answer by unity_sxKFJS36l-DdLQ · Jan 12, 2021 at 08:12 AM

Thanks but problem remains the same.

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.

Follow this Question

Answers Answers and Comments

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

Android Tap detection 2 Answers

Tapping iAd via AdMob is triggering RANDOM clicks on sprites in my scene. 1 Answer

Mobile Tap Issue 1 Answer

Tap to click on Mac touchpad 0 Answers

Player to look at 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