Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

2 quick ?'s GetMouseButtonDown(1) not working in this code and how to use OnMouseDown with istrigger

Discussion in 'Scripting' started by tawdry, Mar 6, 2015.

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    So i have set up a cube that has a collider on and the following code.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class portraits : MonoBehaviour {
    5.     public GameObject name;
    6.     public GameObject weapon;
    7.     public int Health;
    8.     Transform mytransform;
    9.    
    10.      void Start () {
    11.     mytransform = name.transform;
    12.         Health = 100;
    13.  
    14.     }
    15.  
    16.  
    17.     void OnMouseDown(){
    18.  
    19.      
    20.     if (Input.GetMouseButtonDown (1)) {
    21.                     Instantiate (name,mytransform.position,Quaternion.identity);}
    22.         if (Input.GetMouseButtonDown (0)) {
    23.             Instantiate (name,mytransform.position,Quaternion.identity);}
    24.  
    25.  
    26.        
    27.     }
    28. }
    Here.GetMouseButtonDown(0) instantiates on left click but when i change it to GetMouseButtonDown(1) it does nothing on the right click is this supposed to work this way.

    #2 I have raycast ignore istrigger set up in physics but i want onMouseDown to be able to trigger on some objects(istrigger selected); how can i do this or is there some other function i can use?(So OnMouseDown uses rays?);
     
  2. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
  3. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Not sure what your point is in that post as I said GetMouseButtonDown(0) is the only mouse input that works on this script 1 and 2 don;t even fire the function i was wondering why.Also is there a alternative To OnMouseDown where u have ignore raycast ontriggers selected?
     
  4. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. class portraits : MonoBehaviour
    6. {
    7.    public GameObject name;
    8.    public GameObject weapon;
    9.    public int Health;
    10.    Transform mytransform;
    11.  
    12.    private Boolean m_LeftMouseButtonDown = false;
    13.    private Boolean m_RightMouseButtonDown = false;
    14.  
    15.    void Start()
    16.    {
    17.       mytransform = name.transform;
    18.       Health = 100;
    19.    }
    20.  
    21.    void OnMouseDown()
    22.    {
    23.       if (m_LeftMouseButtonDown)
    24.       {
    25.          Instantiate (name,mytransform.position,Quaternion.identity);
    26.       }
    27.  
    28.       if (m_RightMouseButtonDown)
    29.       {
    30.          Instantiate (name,mytransform.position,Quaternion.identity);
    31.       }
    32.    }
    33.  
    34.    void Update()
    35.    {
    36.       if (Input.GetMouseButtonDown(0))
    37.       {
    38.          m_LeftMouseButtonDown = true;
    39.       }
    40.  
    41.       if (Input.GetMouseButtonDown(1))
    42.       {
    43.          m_RightMouseButtonDown = true;
    44.       }
    45.  
    46.  
    47.       if (Input.GetMouseButtonUp(0))
    48.       {
    49.          m_LeftMouseButtonDown = false;
    50.       }
    51.  
    52.       if (Input.GetMouseButtonUp(1))
    53.       {
    54.          m_RightMouseButtonDown = false;
    55.       }
    56.    }
    57. }
     
    Last edited: Mar 6, 2015
  5. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Hey rnakrani I pasted your script in had to // out the update else neither button worked still the right mouse button does not get registered I assume this is incorrect behavior.
     
  6. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Let me take a look. I wrote it in the text editor, not in Unity itself. It should be close.
     
  7. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Wow, I can't believe I never come across this before.

    OnMouseDown only gets called for the left mouse button. Makes sense in one way, maybe not in another way.
     
  8. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Basically, what that means is you need to get rid of the OnMouseDown() stuff and do your code in Update()... with the code I provided for the Update() function.

    My bad. But at least you have a solution! :)
     
  9. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Problem is that right mouse always did work in the update no issue with that its in the OnMouseDown segment I need it to work as it is only to activate when clicking on this object not when clicking anywhere.Which is also why i need a alternative to it as i want to set the object collider to a istrigger which means onmousedown function will not work at all
     
  10. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Can you try putting the code you have in OnMouseDown() into OnMouseOver()?
     
  11. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    Give this a try to demonstrate the distinction between the GetMouseButtonDown() method and the
    OnMouseDown() Event trap.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseDownTester : MonoBehaviour
    5. {
    6.     /*  Attach this Script to a Game object we wish to select/mouseClick on.
    7.      */
    8.     // Use this for initialization
    9.     private void Start() { }
    10.  
    11.     // Update is called once per frame
    12.     private void Update() {
    13.         /*  GetMouseButtonDown(int button);
    14.          *      Returns true during the frame the user pressed the   given mouse button.
    15.          *      You need to call this function from the Update function,  since the state gets reset each frame.
    16.          */
    17.         if (Input.GetMouseButtonDown(0)) {
    18.             Debug.Log("Pressed left click.");
    19.         }
    20.  
    21.         if (Input.GetMouseButtonDown(1)) {
    22.             Debug.Log("Pressed right click.");
    23.         }
    24.  
    25.         if (Input.GetMouseButtonDown(2)) {
    26.             Debug.Log("Pressed middle click.");
    27.         }
    28.     }
    29.  
    30.     private void OnMouseDown() {
    31.         /*  OnMouseDown is called when the user has pressed the
    32.          *  mouse button while over the GUIElement or Collider.
    33.          */
    34.         Debug.Log("OnMouseDown event has fired.");
    35.     }
    36. }
    Note that the OnMouseDown() Event seems to only trap the Left Click.



     
  12. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Brilliant mate that did the trick a gold star for you. Still don't understand why it doesn't work in OnMouseDown maybe someone could provide some insight on that?. Still got the problem of it not reacting if i set the collider to istrigger tried adding a rigidbody to it but that didnt work:( Still good call.
     
  13. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Hey kdub yea but it doesn't allude to that in the documentation kinda a oversight and really it should work seems like a bug to me
     
  14. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    So, there is an OnMouseEnter and an OnMouseExit.

    OnMouseEnter is when the mouse is over the object. No additional raycasts required. You know it is over it. If you know it is over it AND a mouse button is pressed that should be all you need, right?

    OnMouseExit is when the mouse is no longer over the object.

    So here's a possibility.

    Add a Boolean to your class (maybe something like m_Selected). Set it to false in Awake or Start, or where you declare the variable.

    In OnMouseEnter, if the mouse button is down set m_Selected to true.

    In OnMouseExit, set m_Selected to false.

    Then, in the Update function (or other functions in your class) you can do:

    if (m_Selected == true)
    {
    // do stuff
    }
     
  15. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    Gold Star is nice.
    Gold or Beer is nicer :)

    Kerry,
     
  16. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Ok will give it a go Makrani someone sneaked the gold star i awarded to you so I reaward it and some Gold and Beer.as well.
     
  17. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Nope does not work with collider set to trigger looks like all mouse effects use a raycast ?