Unity 3D Tutorial – How to Touch, Tap and Rotate game objects in unity

Welcome to the first Unity 3D mobile development tutorial. In this tutorial we will cover how to touch, tap and rotate game objects in unity.  In this project we will create a simple application for the android framework where you can tap on each object change their individual colour and also apply a smooth rotation to them. This video will go over how to create a Mobile native touch input without using any assets from the asset store for unity also we will cover how to call a function from another games object.

Lesson objectives –

  1. Start a new android project in unity
  2. Create a object prefab and attach rotate box script to it
  3. Attach the touch box script the main camera
  4. Use INPUT and TOUCH functionality in the scripts to identify objects
  5. Use tags in game objects
  6. Access the script and run a function from another game object
  7. Individually select and rotate the cubes on the scene

 

Full Video Tutorial on How to make the touch controls work in unity

 

Touch Box Script

This script below is attached to the main CAMERA. Inside of the update function it will check if it have started the touch input on the device and then it will send out a ray cast to find objects available in the scene and then it will check if those objects have a TAG called BOX, if that is true then it will generate a random colour using the RGB components and the unity random number generator. That random colour will be assigned to the game object and then it will find the rotate box script which is attached to the game object and set the ROTATEME Boolean to true.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TouchBox : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
        if (Input.touchCount>0 && Input.touches[0].phase == TouchPhase.Began)
        {
            
            Ray ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform.tag == "box")
                {
                    
                    Color newColor = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f));
                    hit.collider.GetComponent<MeshRenderer>().material.color = newColor;
                    hit.collider.GetComponent<RotateBox>().ChangeBool();

                }
            }

        }
    }
}

Rotate Box Script

This is the rotate box script that is attached to the CUBE in the game scene. This script has 4 variables 3 for the X Y and Z angle speed also one Boolean to check if this box should rotate or not. We will use this rotate me Boolean from the touch box script to activate or deactivate the object from animating in the scene. Inside the update function it will check if the rotate me Boolean is true then it will change the ySpeed float from 0 to 40 and it will start rotating it in that angle. if that Boolean is set to false it will change the ySpeed float back to 0.  We also have a public function called change bool which will reverse the Boolean for example if that Boolean if set to false then it will reverse it and change it to true and if its set to true it will set it back to false.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RotateBox : MonoBehaviour
{
    float xSpeed = 0.0f;
    float ySpeed = 0.0f;
    float zSpeed = 0.0f;
    public bool RotateMe = false;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
        if (RotateMe)
        {
            ySpeed = 40;
        }
        else
        {
            ySpeed = 0;
        }
        transform.Rotate(
            xSpeed * Time.deltaTime,
            ySpeed * Time.deltaTime,
            zSpeed * Time.deltaTime
        );
    }

    public void ChangeBool()
    {
        RotateMe = !RotateMe;
    }

}

let us know what you think in the comments section below.

 

 




Comments are closed.