Search Unity

How do you set a specific area or zone for OnMouseUp or Down [see picture]

Discussion in 'Scripting' started by softwizz, Jan 7, 2014.

  1. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    Hello.

    Now then I have this situation where the player selects their weapon by clicking a button (GUITexture)

    Easy enough but 2 of the weapons are 'airstrike' type weapons where once selected the player clicks on the screen where the weapon will appear.

    Now the problem is this, if the player selects one of these weapons then changes their mind and decides to select another, when they click the weapon button the weapon previously selected will fire behind the button.

    So how do you restrict or define an area so that in the fire method I can use someting like:
    Code (csharp):
    1. if(Input.GetMouseButtonDown(0)  the area the mouse was clicked in is within these bounds)
    Have a look at this screen shot, the weapon should not fire if the mouse is clicked below the red line. The red line is not part of the scene I just drew it on so you can see what I mean.

    $moses_controls.png

    the 3 sacks are the weapon select buttons


    This is the code for a hailstorm (like an airstrike)
    Code (csharp):
    1. if(Input.GetMouseButtonDown(0)  weaponselected >=2)
    2.         {
    3.             if(weaponselected == 2  HailAmmo >0) // call hailstorm
    4.             {
    5.                 mousePosition = Input.mousePosition; // get the touch position
    6.                 Vector2 p = cam.ScreenToWorldPoint(mousePosition); // create a vector2 converting screen co-ords to world co-ords
    7.                 Instantiate(hailstorm, p, Quaternion.identity); // instantiate the hailstorm at the touch point
    8.                 HailAmmo--; // reduce amount of hailstorms player has
    9.                 weaponselected = 1;
    10.             }
    Any ideas?

    Thanks
     
    Last edited: Jan 7, 2014
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Can't you just check if mousePosition.y is greater than the position of that red line?
     
  3. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    Well it seems that writing the problem down gave me the answer as I just solved it.

    I took the mouse co-ordinates and converted them to world co-ords then just added the check as you suggested to the fire condition.

    Its messy at the moment but I will tidy it up now.

    It appears the line is at world co-ords y = 2.6

    So I used this in the update()
    Code (csharp):
    1. mousePosition = Input.mousePosition;
    and the fire condition becomes
    Code (csharp):
    1. if(Input.GetMouseButtonDown(0)  cam.ScreenToWorldPoint(mousePosition).y > 2.6  weaponselected >=2)
    So yes I can do that.
    Thanks for the reply anyway and some advice for others who get stuck at times, try writing your problem out and the solution might come to you in a flash of obviousness.

    I bet I will dream about coding tonight :sad:
     
    Last edited: Jan 7, 2014
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    If you know how much space your bottom "UI" takes up then you don't even have to convert it to world coordinates.
     
  5. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    I printed out the mouse position to get the value so I didnt need to convert really but I did because this is being developed for Android and at the moment I am not sure how not converting them would have an effect on different screen sizes / resolutions.

    It may be that it will be better to not convert them.

    EDIT:

    The co-ordinates should not be converted to world space as when the player goes up a ladder or whatever the converted co-ordinates change so that setting it as >2.6 means that world co-ords 2.6 are off the bottom of the visible screen at times so the weapon will still fire if clicked in the UI zone.

    Keeping the check at screen co-ordinates stops this as even if the player gous up 100 world units the screen co-ordinate is the same.

    the condition now becomes
    Code (csharp):
    1.     if(Input.GetMouseButtonDown(0)  mousePosition.y > 105  weaponselected >=2)
     
    Last edited: Jan 8, 2014