Unity touch screen touchpad script to transform any objects easily



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:

loading...

Leave a Reply

Your email address will not be published.