- Home /
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);
}
}
}
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?
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()
}
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
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???
Please keep habit of editing answer not create new one each item.
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.
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?
By Using Two Cameras One is For Your GamePlay And Another is For Colliders
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;
}
}