• 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 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
4

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 8 · 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Coding Help 0 Answers

How to use a script on multiple gameobjects, then change a variable for one of them, not the other. 3 Answers

[C#] Translate.position a lot of gameobjects at once (due to velocity limitations) 0 Answers

OnTriggerEnter() not firing for mutliple objects 0 Answers

Multiple mice in Unity 4 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges