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 psajtik · Aug 02, 2015 at 11:51 AM · unity 5onmousedowndebug.logleftmouseclick

Click on cube to Debug.Log

Hi

I am learning Unity3D so i am absolute beginner in this. I have plane (as ground), on plane i have cube with colider and rigidbody and then i have firstperson controller. I want to go with FPC (firstperson controller) to a cube and click on it with left button on mouse and i expect, that gives me a log in console. Script is attached to cube.

Thanks for help

My script code:

 #pragma strict
 
 function OnMouseDown() {
     Debug.Log("Left Click");
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by OrdinaryDev83 · Aug 02, 2015 at 12:59 PM

Use a Raycast using the cursor position from camera to far away:

 #pragma strict

 var hit : RaycastHit;
 //The range of the raycast
 var raycastRange : float;
 var actualCamera : Camera;
 
 function Update () {
     var ray : Ray = actualCamera.ScreenPointToRay (Input.mousePosition);
  
     //Debug the ray in green
     Debug.DrawRay(ray.origin, ray.direction * raycastRange, Color.green);
  
      
     if(Physics.Raycast (ray, hit, raycastRange)) {
         if(Input.GetMouseButtonDown(0)){
             Debug.Log("Touched " + hit.collider.name);
             //You can use tag for it
             if(hit.collider.tag == "YourTagHere"){
                 Debug.Log("Touched " + hit.collider.name + " with the " + hit.collider.tag + " tag.");
             }
         }
     }
 }

Attach this script to camera.

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 psajtik · Aug 02, 2015 at 01:22 PM 0
Share

I tried this solution using Raycast, but doesn't work. I deleted all old scripts on cube, wrote new one with this code, attached is to camera on firstperson controller and nothing. Nothing happend...anyway thanks for reply

avatar image OrdinaryDev83 · Aug 02, 2015 at 01:43 PM 0
Share

EDIT : I added a new variable for selecting THE first person camera, if you use multiples cameras.Test now :) EDIT 2:Have you change the raycast range...? x) set it to 1000 to start.

avatar image psajtik · Aug 02, 2015 at 02:11 PM 0
Share

yes it is working... :) thanks a lot.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Only Allow One Click on GameObject? 2 Answers

Making A Debug.Log depending on what GameObject is active 1 Answer

How do I select a group full of sprites, between two points? 1 Answer

Why my OnMouseDown is lost?!!! 0 Answers

Wondering if I can modify Unity to work better for 1 specific game. 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