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 dado99 · Jun 27, 2014 at 01:42 AM · mouseclickcoordinates

OnMouseDown doesn't work...

everything work fine... but when i click nothing happend... i want: clicking to the left half of the screen my object move left and clicking to the right half of the screen my object move right here my code... what is wrong???

 public Rigidbody2D body;
 public Vector2 forza;
 public Vector2 left;
 public Vector2 right;
 public Vector2 perso;
 private int x=0;
 public int a;
 private int q;
 private Vector2 mouse;
 private double wow;


 // Use this for initialization
 void Start () {
     this.body = this.gameObject.rigidbody2D;
     this.mouse = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
     this.wow = this.mouse.x;
 }


 
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown (KeyCode.UpArrow) && x < a) {
                     this.body.AddForce (forza);
         x=x+1;
             }
     if (Input.GetKeyDown (KeyCode.LeftArrow)) {
                     q = 1;
             }
     if (Input.GetKeyDown (KeyCode.RightArrow)) {
                     q = 2;
             }
     if (Input.GetKeyDown (KeyCode.DownArrow)) {
                     this.body.AddForce (-forza);
             }
     if (q == 1) {
                     this.body.AddForce (left);
             }
     if (q == 2) {
                     this.body.AddForce (right);
             }
     if (Input.GetKeyUp (KeyCode.LeftArrow) || Input.GetKeyUp (KeyCode.RightArrow)) {
                     q = 0;
             }
 
 }
 void OnCollisionEnter2D(){

     x = 0;

             

     }
 void OnMouseDown(){

     if (wow<0) 
     {
                     this.body.AddForce (left);
             } 
     else
     {
         this.body.AddForce (right);
             }
     }

}

Comment
Add comment · Show 3
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 rutter · Jun 26, 2014 at 11:37 PM 1
Share

Try putting a simple Debug.Log message to check if On$$anonymous$$ouseDown is even being called. Does the GameObject you're clicking have this script and a collider attached?

avatar image EvilTak · Jun 27, 2014 at 02:22 AM 0
Share

First of all, as @rutter said, check if On$$anonymous$$ouseDown really works. If it does, then you will have to assign the mouse position vector at the time of On$$anonymous$$ouseDown, since Start() is just used for initialization.

 void On$$anonymous$$ouseDown()
 {
     mouse = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
 
     //Condition check and AddForce()
 }
avatar image dado99 · Jun 27, 2014 at 10:36 AM 0
Share

ok... the void work but i want move my object by pressing to the left/right half of the screen... i want to divide my screen in 2 big button.. and these have to move my object... pls help me

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mehul-Rughani · Jun 27, 2014 at 06:54 AM

OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.-- Have You Attached collider to that gameobject???

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 siddharth3322 · Jul 05, 2014 at 08:37 AM 0
Share

Please keep habit of editing answer not create new one each item.

avatar image
0

Answer by Mehul-Rughani · Jun 30, 2014 at 03:40 AM

Put Two Colliders One is on left side of the screen and another is right side of the screen.

Comment
Add comment · Show 3 · 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 dado99 · Jun 27, 2014 at 11:59 AM 0
Share

mmm... now work bad... i can only press inside my object and when i do it that only move right... is there a method to move my object pressing in the left/right half of the screen?

avatar image dado99 · Jul 01, 2014 at 12:40 PM 0
Share

how can i do it?

avatar image Mehul-Rughani · Jul 05, 2014 at 03:52 AM 0
Share

By Using Two Cameras One is For Your GamePlay And Another is For Colliders

avatar image
0

Answer by RakshithAnand · Jul 01, 2014 at 01:31 PM

You are getting the mouse position in start! Obviously it wont work. Start method is called only once and its used for startup initialization.

You need to get the mouse position in Update method.

try doing this:

 void Update()
 {
    if(Input.GetMouseDown(0))
    {
       Vector2 mousePos = Input.mousePosition;
       this.wow = mousePos.x;
    }
 }
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

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

6 People are following this question.

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

Related Questions

Get Pixel Coordinate When Click On 2D Object 1 Answer

Arrays vs Coordinates 2 Answers

How can I match the position of my mouse cursor to the position of my texture? 0 Answers

Information about animation coordinates in real time. 1 Answer

Left-handed coordinates to right-handed coordinates 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