How to trace the touch

Hi everyone!

I’m a newb to unity and trying to develop a letter tracing app for touch devices. For that I need a way to show what user is writing on the screen. My idea is to represent a touch as a colored ‘dot’ so user can see what he/she has written. Can you give me some idea how to do this?

The most efficient solution is to create a mesh composed of individual quads. Each quad is a dot. The mesh is dynamically created/extended to represent your dots. This is more advanced programming task. But if you are willing to spend a bit of $$, it can easily be done with Vectrosity from the Asset store. As a single mesh with a single material, the dots will be drawn as a single draw call.

As an alternate (and less efficient soluton), you can create objects to represent your dots. Here is a bit of example code. Start a new scene, create an empty game object and attach this script:

#pragma strict

var currPos : Vector3;
var camDist = 10.0;
var dotDist = 0.2;

function Update () {

	if (Input.GetMouseButtonDown(0)) {
	    var pos = Input.mousePosition;
	    pos.z = camDist;
		currPos = Camera.main.ScreenToWorldPoint(pos);
		var go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
		go.transform.localScale = Vector3(0.1,0.1,0.1);
		go.transform.position = currPos;
	}
	
	if (Input.GetMouseButton(0)) {
		pos = Input.mousePosition;
	    pos.z = camDist;
		pos = Camera.main.ScreenToWorldPoint(pos);
		if (Vector3.Distance(pos, currPos) > dotDist) {
			go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
			go.transform.localScale = Vector3(0.1,0.1,0.1);
			go.transform.position = pos;
			currPos = pos;
		}
	}
}

This is obviously a Mouse solution that needs to be converted to touch, plus you’ll like want to either Instantiate or create Quads with a dot texture rather than use a sphere as I’ve done for demo purposes. Drawing the dots should batch, so even for mobile you should be able to put a reasonable number of dots down before you have FPS issues.

If you don’t need the indicator to stick around, consider a TrailRenderer.