Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

Unity touch screen touchpad script to transform any objects easily

Posted on March 4, 2021March 4, 2021 by Habibie

In this blog post I will share a new script that I’ve made to create a touch screen touchpad in our Unity game to control object’s transformations such as position, rotation and scale, easily.

I call this script TNObjectTransformTouchpad.

Here is it:

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

public class TNObjectTransformTouchpad : MonoBehaviour
{
	[Header("Target Object")]
	public GameObject targetObject;
	
	[Header("Touchpad Direction")]
	public bool horizontal;
	public bool vertical;
	
	[Header("Movement")]
	public bool toMove;
	public bool movementX;
	public bool movementY;
	public bool movementZ;
	
	[Header("Scale")]
	public bool toScale;
	public bool scaleUniform;
	public bool scaleX;
	public bool scaleY;
	public bool scaleZ;
	
	[Header("Rotation")]	
	public bool toRotate;
	public bool rotateX;
	public bool rotateY;
	public bool rotateZ;
	public bool globalRotation;
	
	[Header("Speed Increment")]
	public float speed = .1f;
	
	bool ismouseheld;
    Vector2 currentMousePosition;
    Vector2 mouseDeltaPosition;
    Vector2 lastMousePosition;
    bool istouchpadactive;
	
    void Start(){
        ResetMousePosition();
    }
	
	void FixedUpdate()
    {
        if (istouchpadactive){
            currentMousePosition = Input.mousePosition;
            mouseDeltaPosition = currentMousePosition - lastMousePosition;
            
			if(toMove){
				if(movementX){
					if(horizontal)
						targetObject.transform.Translate(mouseDeltaPosition.x * speed, 0f, 0f);
					if(vertical)
						targetObject.transform.Translate(mouseDeltaPosition.y * speed, 0f, 0f);
				}
				if(movementY){
					if(horizontal)
						targetObject.transform.Translate(0f, mouseDeltaPosition.x * speed, 0f);
					if(vertical)
						targetObject.transform.Translate(0f, mouseDeltaPosition.y * speed, 0f);
				}
				if(movementZ){
					if(horizontal)
						targetObject.transform.Translate(0f, 0f, mouseDeltaPosition.x * speed);
					if(vertical)
						targetObject.transform.Translate(0f, 0f, mouseDeltaPosition.y * speed);
				}
			}
			
			if(toScale){
				if(scaleUniform){
					if(horizontal)
						targetObject.transform.localScale += new Vector3(mouseDeltaPosition.x * speed, mouseDeltaPosition.x * speed, mouseDeltaPosition.x * speed);
					if(vertical)
						targetObject.transform.localScale += new Vector3(mouseDeltaPosition.y * speed, mouseDeltaPosition.y * speed, mouseDeltaPosition.y * speed);
				}else{
					if(scaleX){
						if(horizontal)
							targetObject.transform.localScale += new Vector3(mouseDeltaPosition.x * speed, 0, 0);
						if(vertical)
							targetObject.transform.localScale += new Vector3(mouseDeltaPosition.y * speed, 0, 0);
					}
					if(scaleY){
						if(horizontal)
							targetObject.transform.localScale += new Vector3(0, mouseDeltaPosition.x * speed, 0);
						if(vertical)
							targetObject.transform.localScale += new Vector3(0, mouseDeltaPosition.y * speed, 0);
					}
					if(scaleZ){
						if(horizontal)
							targetObject.transform.localScale += new Vector3(0, 0, mouseDeltaPosition.x * speed);
						if(vertical)
							targetObject.transform.localScale += new Vector3(0, 0, mouseDeltaPosition.y * speed);
					}
				}
			}
			
			if(toRotate){
				if(rotateX){
					if(globalRotation){
						if(horizontal)
							targetObject.transform.Rotate(mouseDeltaPosition.x * speed, 0, 0, Space.World);
						if(vertical)
							targetObject.transform.Rotate(mouseDeltaPosition.y * speed, 0, 0, Space.World);
					}else{
						if(horizontal)
							targetObject.transform.Rotate(mouseDeltaPosition.x * speed, 0, 0);
						if(vertical)
							targetObject.transform.Rotate(mouseDeltaPosition.y * speed, 0, 0);
					}
				}
				if(rotateY){
					if(globalRotation){
						if(horizontal)
							targetObject.transform.Rotate(0, mouseDeltaPosition.x * speed, 0, Space.World);
						if(vertical)
							targetObject.transform.Rotate(0, mouseDeltaPosition.y * speed, 0, Space.World);
					}else{
						if(horizontal)
							targetObject.transform.Rotate(0, mouseDeltaPosition.x * speed, 0);
						if(vertical)
							targetObject.transform.Rotate(0, mouseDeltaPosition.y * speed, 0);
					}
				}
				if(rotateZ){
					if(globalRotation){
						if(horizontal)
							targetObject.transform.Rotate(0, 0, mouseDeltaPosition.x * speed, Space.World);
						if(vertical)
							targetObject.transform.Rotate(0, 0, mouseDeltaPosition.y * speed, Space.World);
					}else{
						if(horizontal)
							targetObject.transform.Rotate(0, 0, mouseDeltaPosition.x * speed);
						if(vertical)
							targetObject.transform.Rotate(0, 0, mouseDeltaPosition.y * speed);
					}
				}
			}
			lastMousePosition = currentMousePosition;
        }
    }
	
	public void ResetMousePosition(){
        currentMousePosition = Input.mousePosition;
        lastMousePosition = currentMousePosition;
        mouseDeltaPosition = currentMousePosition - lastMousePosition;
    }
	
	public void TouchpadActivate(){
        ResetMousePosition();
        istouchpadactive = true;
    }
 
    public void TouchpadDeactivate(){
        istouchpadactive = false;
    }
	
}

How to use it?

First make an UI Image on your Unity canvas as your touchpad. Then attach this script to it. Add an event system component to it. Add Pointer Down and Pointer Up events. For Pointer Down, call TouchpadActivat() method, and for Pointer Up call TouchpadDeactivate() method of this script.

Don’t forget to drag and drop your target object to the slot in the script.

Just like this:

Post Views: 417
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
  • Advanced Blinking Marker Script to show objects position in your game canvas
  • Ciihuy Images Merger – Fast & Easy Online Image Combiner
© 2025 ThirteeNov | Powered by Superbs Personal Blog theme