Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
3
Question by Gorang Gupta · Jan 28, 2015 at 07:23 AM · mousegameobjectsmultiple

how to detect an gameobject on mouse click using raycast

i have multiple objects running on single script. i want to select one object on mouse click.

 if ( Input.GetMouseButtonDown (0)){ 
 RaycastHit hit; 
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
 if ( Physics.Raycast (ray,out hit,100.0f)){ 
 //suppose i have two objects here named obj1 and obj2.. how do i select obj1 to be transformed 
 if(hit.transform!=null) { 
 transform.Translate (Time.deltaTime, 0, 0, Space.Self); 
 } 
 }


}

The answers given previously do not work.. pls help

Comment
Add comment · Show 1
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 Gorang Gupta · Jan 29, 2015 at 05:45 AM 0
Share

hey thanks for it.. it does not work.....

1 Reply

· Add your reply
  • Sort: 
avatar image
5

Answer by hav_ngs_ru · Jan 28, 2015 at 10:04 AM

actualy you doind right to detect what object was picked... but I didnt understand what are you trying to do with selected object...

transform.Translate (Time.deltaTime, 0, 0, Space.Self) - this will move selected object along X just about 0.03 units (so you cant see it). Try something like this instead:

 if ( Input.GetMouseButtonDown (0)){ 
   RaycastHit hit; 
   Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
   if ( Physics.Raycast (ray,out hit,100.0f)) {
     StartCoroutine(ScaleMe(hit.transform));
     Debug.Log("You selected the " + hit.transform.name); // ensure you picked right object
   }
 }
 
 IENumerable ScaleMe(Transform objTr) {
     objTr.localScale *= 1.2f;
     yield return new WaitForSeconds(0.5f);
     objTr.localScale /= 1.2f;
 }
Comment
Add comment · Show 9 · 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 Gorang Gupta · Jan 29, 2015 at 05:46 AM 0
Share

hey.. thats true.. but if i continuously press the right key it should move na.

avatar image hav_ngs_ru · Jan 29, 2015 at 09:30 AM 0
Share

where do you want it to move?

ok, I'm a user. I see 2 obkects. What I should click some of them for? In other words - when I clicked some of them - what I expect to happen?

avatar image Gorang Gupta · Jan 30, 2015 at 03:39 AM 0
Share

i would want the obj1 or obj2 to move either right or left on click.

avatar image hav_ngs_ru · Jan 30, 2015 at 03:37 PM 1
Share

so, for continiousl button press handilng - use Get$$anonymous$$ouseButton ins$$anonymous$$d of Get$$anonymous$$ouseButtonDown (first will return true every frame while mouse button is held down, but first - only once when you press it down, but not when you hold it). And set speed to some value more about 10, to ensure you wil really see if the motion occured.

 if ( Input.Get$$anonymous$$ouseButton (0)){ // << use Get$$anonymous$$ouseButton ins$$anonymous$$d of Get$$anonymous$$ouseButtonDown
    RaycastHit hit;
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if ( Physics.Raycast (ray,out hit,100.0f)) {
       Debug.Log("You selected the " + hit.transform.name); 
       hit.transform.position += Vector3.right * speed * time.deltaTime; // << declare public speed and set it in inspector
    }
 }
avatar image Gorang Gupta · Jan 30, 2015 at 04:22 PM 0
Share

will try and let u know

Show more comments

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple GameObjects with the same script all react but I only want one at a time. 2 Answers

Create multiple prefabs from multiple objects in the selection of the Hierarchy (Unity 5.3.5) 1 Answer

Destroying objects with the same name that are touching eachother. 1 Answer

Batching objects with Instantiate for optimization 2 Answers

How to detect range of multiple gameobjects? 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