Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
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
Add comment · 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 screa$$anonymous$$g "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

· Add your reply
  • Sort: 
avatar image
7
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
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
12

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
Add comment · 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 $$anonymous$$ouse Position from the $$anonymous$$ain 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
4

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
Add comment · 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.

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

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

Stop animation on the time given then start animation when press a key 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Raycast play animation1 and then animation2 1 Answer

Animation Script Problem. 1 Answer

Field of view scripted animation curve 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