Click the video to watch.

New Input System

If you want to destroy a game object by tapping in Unity, follow these simple steps and you will perform a destroy tap! Since the New Input System is a default choice for my tutorials, I will first teach you how to implement a simple system for tapping and clicking in Unity. The destroy tap feature is very useful, so you can learn how to add it into your game!

If you already have the new input system package installed in Unity, all you have to do is to add a new input actions asset, generate C# class and add a new layer called “Click”.

destroy tap feature

Inside that action layer, add an action called “Touch”. This action will include only two bindings: Mouse button and screen tap. At first, you need to make the introduction to the new input system. You can do it in void Awake(), simply by saying input = new InputSystem().

Implementation

It is understandable that the input system needs to exist in your project in order to work. First of all, you need to add an input actions asset that will consist of one action layer – Click. It needs to have two bindings in the action called Touch. The Input System will be enabled on game launch, but disabled when it is game over. You have to call input = new InputSystem(); in the main part of the script, which is void Awake() – a void that is called before Start(). After adding two bindings, you need to define the syntax.

The syntax

void Awake(){
    input = new InputSystem();
    input.Click.Touch.performed += OnTap;
}

void OnEnable(){
    input.Enable();
    input.Click.Touch.performed += OnTap;
}

void OnDisable(){
    input.Disable();
    input.Click.Touch.performed -= OnTap; //pay attention!
}

void OnTap(InputAction.CallbackContext ctx)
{
    if(ctx.performed)
    {
        //do something
    }
}

Now it is time to add the input action. Do it simply by adding input.Click.Touch.performed. You need to define what action do you want to be performed. It will be called OnTap. Make sure to put a Callback Context, and an if statement that checks if you click on the screen. You need to consider adding an if-statement that will check if you are pointing on the screen. These images are considered a valid explanation of each component that is important in this small project:

How to implement mouse cursor

The parts of the script are defined with raycasting. You need to cast the screen to world point if you want to instantiate the game object that will symbolize the cursor. The reason why you also need a circle object is because it will be an aiming cursor every time you click on the screen. The mouse cursor can be defined with the ScreenPointToRay, which gives a quick conversion from the viewport into the mouse position. This differs a lot from the ScreenToWorldPoint, which will be explained as the way to instantiate a virtual cursor object in your viewport.

How to instantiate the cursor object

Adding a small aim object, which is a circle, can be an interesting feature. This game requires a small object that defines the position of your mouse. If it touches the box object, it will disappear.

Instantiate(shootObject, ScreenToWorldPoint(Input.mousePosition), Quaternion.identity);

This is the syntax of Instantiate component in C#.

The void OnMouseDown() is still considered old input system, but it can be useful for detecting if you are tapping the object. Inside that void, simply put Destroy(gameObject) and that will work fine. After that, you need to make sure that bool and gameObject are also in your project. After instantiating, the boolean will remain false, but the object will be visible on the screen.

What next?

You also need a small script that can remove the cursor every time a specific time passes by. Here is the script:

public float timeToDestroy;

void Start(){
    Invoke("destroyOnTime", timeToDestroy);
}

void destroyOnTime(){
    Destroy(gameObject);
}

There is no need to explain, because the script basically calls the destroyOnTime function after the number of seconds that pass is equivalent to timeToDestroy. The full scripts for this mini project will be posted on the creator’s Github. If you find this video helpful, consider subscribing and you will be notified when new tutorials will be released.

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Comments

  1. Nice idea! This character can be used in order to make a very interesting game with logos. When I finish…