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.

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.

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



Comment on this tutorial and let us know how you got on -

%d bloggers like this: