Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to detect if a GameObject is begin touched and hold down?

Discussion in 'Getting Started' started by EricMacau, Jan 21, 2015.

  1. EricMacau

    EricMacau

    Joined:
    Nov 8, 2014
    Posts:
    8
    Hello,

    How can I detect if a GameObject is beging touched on and hold down?

    Would you please to teach me if the GameObject is keep touch, just print out "holding", and if it is not be touching, print out "releaseing"?


    Thank you very much.

    Best regards,
    Eric
     
  2. CStunner

    CStunner

    Joined:
    Jul 12, 2013
    Posts:
    39
    What do you mean by "hold down"?

    You add proper 2D collider component or if you want Trigger, set the Is Trigger to true for that collider component (tick that box).
    Then write this function:

    Code (CSharp):
    1. void OnCollisionStay2D (Collision2D other)
    2. {
    3.      Debug.Log ("In Collision");
    4. }
    For getting collision event on Entry and Exit, use: OnCollisionExit2D, OnCollisionEnter2D.

    Here's the Unity Documentation: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionStay2D.html

    And also, at least one of the two Gamebojects MUST have a Rigidbody2D component, for Collision or Trigger..