• 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
Question by Jammer3000 · Nov 05, 2013 at 04:29 AM · animation

How to click a 3d object in unity3d?

Hi can anyone point me to or tell me what code to use if say I have a cube in my scene and i want it to run an animation when i click it? I know how to do this with gui but not with a actual 3d object in unity like a plane or something? Thanks (:

Comment
paladim
JoshCaladia

People who like this

2 Show 7
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 clunk47 · Nov 23, 2013 at 01:58 AM 0
Share

Dude are you going to accept one of these three good answers???

avatar image Huacanacha · Nov 23, 2013 at 07:46 AM 0
Share

24 questions, 0% accept rate. So I'm guessing not...

avatar image fafase · Nov 23, 2013 at 07:55 AM 0
Share

Accept rate is not about how much he accepts, it is about how much his answers have been accepted. Running after acceptance is useless, I have hundreds of questions where the guy said Thanks but no tick, or simply used the answer and left. At first I could not sleep because of this, waking up sweaty in the middle of the night screaming "Tick it will ya!!!!", then I have learnt to live with it.

avatar image clunk47 · Nov 23, 2013 at 09:46 PM 0
Share

I wouldn't say accepting is useless. It lets others know that there has been a proper solution given. Huacanacha, +1.

avatar image fafase · Nov 24, 2013 at 07:46 AM 1
Share

I do not mean accepting is useless, I mean waiting for the guy to accept is most of the time vain. But sure accepting let others know what help to the issue and should be done.

Show more comments

3 Replies

  • Sort: 
avatar image
Best Answer

Answer by Huacanacha · Nov 05, 2013 at 04:37 AM

You raycast from the click position (mouse or touch etc), then if you hit an object you want to interact with run whatever code you need.

For starters look at: http://docs.unity3d.com/Documentation/ScriptReference/Input-mousePosition.html http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

You can use a layer mask for Physics.Raycast so you only check the objects you are interested in, or you could use RaycastAll etc.

Comment
Dismortus
Jammer3000
martin-rohwedder
jovino
ejpaari
clunk47
ryan_mclaughlin
jacobrutter

People who like this

8 Show 0 · 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

Answer by clunk47 · Nov 05, 2013 at 04:41 AM

You could use OnMouseDown, or Raycast to do this, be sure to have a collider of some type on the object for either.

OnMouseDown example in C#

Attach this to the object you want detected:

 using UnityEngine;
 using System.Collections;
 
 public class OnMouseDownExample : MonoBehaviour 
 {
     void OnMouseDown()
     {
         print (name);    
     }
 }



Raycast example in C#

attach this to any object. Just to make sense, attach to the main camera or an empty.

 using UnityEngine;
 using System.Collections;
 
 public class RaycastExample : MonoBehaviour 
 {
     Ray ray;
     RaycastHit hit;
     
     void Update()
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(ray, out hit))
         {
             if(Input.GetMouseButtonDown(0))
                 print(hit.collider.name);
         }
     }
 }
 
 
Comment
Dismortus
Jammer3000
martin-rohwedder
Droidabi
esoinila
eyupunity
Kekaku
ryan_mclaughlin
Powzone
unity_R3CKk5yG4qmT5w
MistyPine
JoshCaladia
jacobrutter

People who like this

13 Show 7 · 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 Jammer3000 · Nov 13, 2013 at 05:31 PM 0
Share

Thanks it works but is there away i could make it so it only works if the object I'm clicking on has a certain tag and I'm using the raycast way but in javaScript???

avatar image clunk47 · Nov 13, 2013 at 08:54 PM 0
Share

Very simple.

 #pragma strict
 
 var ray : Ray;
 var hit : RaycastHit;
 
 function Update () 
 {
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if(Physics.Raycast(ray, hit))
     {
         if(hit.collider.tag == "Enemy")
             print(hit.collider.name);
     }
 }
avatar image Jammer3000 · Nov 13, 2013 at 09:39 PM 0
Share

Thanks quick question what are var ray : Ray; var hit : RaycastHit; for?

avatar image clunk47 · Nov 13, 2013 at 09:54 PM 0
Share

Look it up in Unity Script Reference man. Ray is a RAY casting from your Mouse Position from the Main camera in this case. RaycastHit is what the endpoint of the ray is HITTING. Links provided below for more information on Unity Script Reference.

RaycastHit

Ray

avatar image clunk47 · Nov 14, 2013 at 07:43 PM 0
Share

If something answers your question, please accept and vote up. If you have more questions, please ask them as separate questions.

Show more comments
avatar image

Answer by Eric5h5 · Nov 05, 2013 at 04:41 AM

Use the OnMouseDown function in a script, and attach the script to the object.

Comment
Dismortus
Jammer3000
martin-rohwedder
clunk47

People who like this

4 Show 2 · 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 Jammer3000 · Nov 05, 2013 at 05:02 AM 0
Share

Attach it to which object? The one I'm clicking or an empty one with a variable that I can assign what object I want to be affected when clicked??

avatar image Eric5h5 · Nov 05, 2013 at 05:06 AM 0
Share

To the object you're talking about in your question.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

How do I add NPCs? 2 Answers

i need a help in script to move while he is animation? 2 Answers

Raycast play animation1 and then animation2 1 Answer

Player is running just few seconds 0 Answers

animated sword + script 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