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

Enable BoxCollider2D only while Mouseclick

Discussion in 'Physics' started by guut, Mar 16, 2017.

  1. guut

    guut

    Joined:
    Mar 16, 2017
    Posts:
    1
    Hi all,

    I´m completely new to Unity and have only basic programming language experience (Java, C++). I have read a few Books and a lot of online tutorials about coding games with C++ or with Unity.

    I really loved the fly swatter mini game within Mario Paint, called "Gnat Attack". Therefore I´m currently developing a game which should be very similar to Gnat Attack.

    What I have accomplished so far:

    • Two different enemy Sprites (A spider enemy type, and a fly enemy type including animation for both).
    • Spawn mechanism two create instances of enemy at random spaces on the screen.
    • Each level has specific amount of enemy waves. The higher the level the more and faster enemys
    • Sprite of the player (Swatter) including animation. The Swatter can be moved around the screen by mouseclick or touch (on Android devices).
    • The enemys are disappearing on mouseclick or touch input
    Everything is working fine, besides of the collision with the colliders. The swatter have a Box Collider on it, which should only be enabled if I click with the mouse on the screen or touch the screen.

    I have tried it with following code:

    public class Kill : MonoBehaviour
    {
    BoxCollider2D boxCol;

    void Start()
    {
    boxCol = GetComponent<BoxCollider2D>();
    }

    void Update () {
    if (Input.GetMouseButtonDown(0)) {
    boxCol.isTrigger = false;
    }
    if (Input.GetMouseButtonUp(0))
    {
    boxCol.isTrigger = true;
    }
    }

    void OnCollisionEnter2D(Collision2D otherCol)
    {

    //Debug.Log(otherCol.gameObject.name);
    if (otherCol.gameObject.CompareTag("Enemy"))
    {
    otherCol.transform.gameObject.SendMessage("KillMe", 0, SendMessageOptions.DontRequireReceiver);
    }
    }

    }


    But this isn´t working that well. If i click on the screen, I can see in the inspector of the box collider, that the trigger is beeing disabled for a very short time and is set back to true afterwards. I can only "destroy" enemys if I hold the mouse button and scroll over them with the mouse(Swatter).

    What would be a better solution?

    This is the code of my Swatter script:

    using UnityEngine;
    using System.Collections;

    public class SwatterController : MonoBehaviour
    {
    public Animator anim;
    bool doInputChecking = true;
    public float speed = 1.5f;
    private Vector3 target;

    void Start()
    {
    target = transform.position;

    }

    void Update()
    {


    if (Input.touchCount > 0)
    {
    if (Input.GetTouch(0).phase == TouchPhase.Began)
    CheckInput(Input.GetTouch(0).position);
    target = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    target.z = transform.position.z;
    }
    else
    {
    if (Input.GetMouseButtonDown(0))
    CheckInput(Input.mousePosition);
    target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    target.z = transform.position.z;
    }
    transform.position = Vector3.MoveTowards(transform.position, target, speed * 5);
    }

    void CheckInput(Vector3 pos)
    {

    if (doInputChecking)
    {
    Vector3 wp = Camera.main.ScreenToWorldPoint(pos);

    Vector2 touchPos = new Vector2(wp.x, wp.y);

    anim.SetTrigger("Click");


    Collider2D otherCol = Physics2D.OverlapPoint(touchPos);

    if (otherCol)
    {
    //Debug.Log(otherCol.gameObject.name);
    if (otherCol.gameObject.CompareTag("Enemy"))
    {
    otherCol.transform.gameObject.SendMessage("KillM", 0, SendMessageOptions.DontRequireReceiver);
    }
    }
    }
    anim.SetTrigger("Release");
    }


    void StopInputChecking()
    {
    doInputChecking = false;
    }


    }
     
  2. ShadyProductions

    ShadyProductions

    Joined:
    Jul 21, 2014
    Posts:
    18
    You will have to use MouseButtonDown event and use an coroutine to time it.
    You want to disable it by default, and when clicked enable it (swat with the swatter and then disable it automaticly after maybe 0.5 seconds)

    You can use a coroutine for this
    Something like this:

    if (Input.GetMouseButtonDown(0)) {
    boxCol.isTrigger = true;
    StartCoroutine(Wait(0.5f));
    boxCol.isTrigger = false;
    }


    private IEnumerator Wait(float waitTime) {
    yield return new WaitForSeconds(waitTime);
    }
     
  3. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350