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
0
Question by perfectskills · Apr 03, 2013 at 10:24 PM · objectclickonmousedownactionsactionscript

how do an action onclick an object?

none of this work for me

      example(1)  


if(Input.GetMouseButtonDown(0))

 transform.Translate(-Vector3.forward * movespeed * Time.deltaTime);  

}

      example(2)  


 function OnMouseDown()  

 {  

 transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);  

 }.  


  


i want to click an object and it do an action like rotate or translate,
the example (1) do the action no matter were i click,
and the example (2) just don't work

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Fehr · Apr 05, 2013 at 12:27 AM

Hey perfectskills.

A RayCast can be used to return the object clicked, I think this may be what your looking for:

 using UnityEngine;
 using System.Collections;
 
 public class RaycastClickExample : MonoBehaviour {
     void Update() {
         float turnSpeed = 45.0f;
         if (Input.GetMouseButtonDown(0)) {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit))
                 if (hit.transform != null) {
                     Debug.Log("Hit " + hit.transform.gameObject.name);
                     hit.transform.Rotate(Vector3.up * turnSpeed);
                 }
         }
     }
 }

Here it is in UnityScript, as that's the form you used in your samples:

 #pragma strict
 class RaycastClickExample : MonoBehaviour {
     var : turnSpeed : float = 45.0;
     function Update () {
         if (Input.GetMouseButtonDown(0)) {
             var hit: RaycastHit;
             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
             if (Physics.Raycast(ray, hit)) {
                 if (hit.transform != null){
                     Debug.Log("Hit " + hit.transform.gameObject.name);
                     hit.transform.Rotate(Vector3.up * turnSpeed);
                 }
             }
         }
     }
 }

Please note that for this to work, there must be a Collider for your RayCast to hit attached to any object you intend to click.

Hopefully this helps!

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
1

Answer by robertbu · Apr 03, 2013 at 11:30 PM

Note that both of these (Input.GetMouseButtonDown() and OnMouseDown()) will only fire once for the single frame that the mouse is down. If you want continuous motion while the mouse is down, use Input.GetMouseButton() or for example #2, use OnMouseDrag(). Note that the OnMouse*() methods require the object you click on to have a collider and the collider must the the front-most collider.

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 perfectskills · Apr 04, 2013 at 11:15 PM 0
Share

ok but how can i do that, i mean how can i clic exactly on an object an make an action, cause no matter where i click on the screen the object always do the action, i want to clic exactly on an oject and do an action, how?

avatar image robertbu · Apr 05, 2013 at 05:50 AM 0
Share

You can do it one of two ways. First On$$anonymous$$ouseDown() will only be called if you click on the collider of the object that has the script. So if you use On$$anonymous$$ouseDown() and are getting clicks off the object, it means you have a collider issue (the collider is too big and/or not aligned to the object).

The second way is to check the name or the tag after you have done the Raycast(). After the raycast you can do something like:

 if (hit.collider.name == "Button") {
     // Do whatever you want
 }

"Button" here is a placeholder for whatever you named your object(s).

avatar image
0

Answer by hoy_smallfry · Apr 03, 2013 at 11:03 PM

Trying using this instead:

 transform.Rotate(Vector3.up * turnSpeed * Time.deltaTime);  
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 perfectskills · Apr 04, 2013 at 11:11 PM 0
Share

well, my question is how can i click on an object an do an action, i know how rotate works, but i want to click only an object, cause no matter what part of the screen i click it always do the action and that is my problem

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

13 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

Related Questions

How to make object clickable when user enter collider ? 0 Answers

Can I detect detect if I clicked on same spot which is enlighted by spotlight? 0 Answers

Run script when object is clicked 1 Answer

Moving an object towards a clicked object 1 Answer

How do I click an object and have it go to another object 3 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