Draw/Write using Raycast in Unity.

so the task pretty much is that I have a panel attached

in the scene . what I have to use mouse (or a pen if available ) and write upon that word that is being displayed .
this is a kind of therapy or physiotherapy tool .
only problem is that the the raycast connects the initial and final points . i.e it picks up from where the pointer was initially left of . is it possible that the raycast can have any arbitrary position but still initial and final point don’t connect ?

the script is attached below  :-



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

public class code : MonoBehaviour {

	
	
	// Update is called once per frame
	private void Update () {
		if(((Input.touchCount>0&&Input.GetTouch(0).phase==TouchPhase.Moved)
            || Input.GetMouseButton(0)))
        {
            Plane objPlane = new Plane(Camera.main.transform.forward * -1, this.transform.position);
            Ray mRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            float rayDistance;
            if (objPlane.Raycast(mRay, out rayDistance))
                this.transform.position = mRay.GetPoint(rayDistance);
        }
	}
}


  
and in this picture pink colored writing is done by me and the black is the writing that is display upon which we have to write.

@Harinezumi @Cresspresso

When you stop touching (or release the mouse button), you should stop editing the old line (I assume you are using a LineRenderer). Then when you start a different touch you should create a new LineRenderer which starts at that position and follows the touch until the touch is finished.


EDIT

Attach this script to any object (e.g. the main camera), and assign it a prefab for your trail renderer. When you hold the left mouse button (input axis “Fire1”) it will draw a line, and when you stop holding it will stop drawing that line. Then when you hold down a second time it creates a new instance of the prefab and uses that trail renderer to draw.

LinePainter.cs

using UnityEngine;

public class LinePainter : MonoBehaviour
{
	public GameObject trailRendererPrefab;
	public float distance = 1f;

	private GameObject currentTrailRenderer = null;

	void Update ()
	{
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		Vector3 pos = ray.GetPoint(distance);

		if (currentTrailRenderer)
		{
			// move trail renderer
			currentTrailRenderer.transform.position = pos;
			currentTrailRenderer.transform.rotation = transform.rotation;
		}

		// stopped holding button
		if (Input.GetButtonUp("Fire1"))
		{
			// release trail renderer
			if (currentTrailRenderer)
			{
				currentTrailRenderer = null;
			}
		}
		// started holding button
		if (Input.GetButtonDown("Fire1"))
		{
			if (trailRendererPrefab)
			{
				// create new trail renderer
				currentTrailRenderer = (GameObject)Instantiate(trailRendererPrefab, pos, transform.rotation);
			}
		}
	}
}