• 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 /
  • Help Room /
avatar image
Question by yade_123 · Oct 07, 2015 at 12:07 PM · c#scripting problem2d game

How to get the object to move only when I click on it and not just anywhere else?

Hi everyone i was trying to make a 2D game where mt gameobject(key) moves when I click on it.Well I was able to move it using the c# script below but, the problem is that when I add more than one object and when i click they all move at once. I just want them to move only I click on them and not just anywhere else on the screen not even on any other object. Please note that I am not using Transform since I am dealing with collisions and I don't my object to shake on collision . ANY HELP IS APPRECIATED. THE SCRIPT-- using UnityEngine; using System.Collections;

 public class movement2 : MonoBehaviour {
 
     void Start () {
     
     }
 
     void Update () {
         if (Input.GetMouseButton(0)) {
             GetComponent<Rigidbody2D>().AddForce (new Vector2(-3,0));
         }
     
     }
 }
Comment

People who like this

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

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Oribow · Oct 07, 2015 at 02:14 PM

If you place the script on each GameObject you want to drag, you can use OnMouseOver() it's getting called on the GameObject, when the mouse is over it.

Comment

People who like this

0 Show 6 · 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 yade_123 · Oct 07, 2015 at 03:31 PM 0
Share

THANK YOU solved half of my problem. I have the same problem in this case but here my object will move on click only when it detects collision with the object"LOCK" . Any idea how I can to do it in this case?

 using UnityEngine;
 using System.Collections;
 
 public class collision2 : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
 
     void OnCollisionStay2D(Collision2D coll)    
     {
         if (coll.gameObject.tag == "LOCK") {
             if (Input.GetMouseButton (0)) {
                 GetComponent<Rigidbody2D> ().AddForce (new Vector2 (18, 0));
             }
         }
     }
 
 }
 

avatar image Oribow yade_123 · Oct 07, 2015 at 04:37 PM 0
Share

Your second script will only react if the GameObject it is on collide with a GameObject that has the tag == "LOCK". But I dont understand what you want. Could you explain more how you want the script to behaive?

avatar image yade_123 Oribow · Oct 08, 2015 at 03:12 PM 0
Share

So I will explain it with this picture. If you need any more information please ask me, and thank you for helping me out.alt text

om.png (26.4 kB)
Show more comments
Show more comments

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

32 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 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

Cannot delay player respawn. 2 Answers

How to activate an animation after a character is idle for a few seconds 1 Answer

Help! Values from previous Serialized List<> shows up again after a while using Coroutine 0 Answers

Can somebody help me fix this script? 2 Answers

Dialogue script shows previous sentences after a while 0 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