I want to draw a straight line and i want these line to continue drawing to the end
This Will Work Like:
- If the touch is a new one, we create
a new trail and associate it to the
finger via the dictionary. - When the finger moves, if we have a
trail associated to it, we also move
the trail’s game object. - When the finger is released, we
destroy the trail.
You can Download Trail GameObject from here.[91303-trail.zip*_|91303]
add a new member that will contain the association between a finger and a trail:
private Dictionary<int, GameObject> trails = new Dictionary<int, GameObject>();
Then, inside Update() Look for all fingers and Make a line.
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine;
public class MakingTrail : MonoBehaviour
{
private readonly Dictionary<int, GameObject> _trails = new Dictionary<int, GameObject>();
[NotNull, SerializeField] private GameObject _trailPrefab;
private void Update()
{
// Look for all fingers
for (var i = 0; i < Input.touchCount; i++)
{
var touch = Input.GetTouch(i);
if (touch.phase == TouchPhase.Began)
{
// Store this new value
if (_trails.ContainsKey(i) == false)
{
var position = Camera.main.ScreenToWorldPoint(touch.position);
position.z = 0; // Make sure the trail is visible
var trail = MakeTrail(position);
if (trail != null)
{
Debug.Log(trail);
_trails.Add(i, trail);
}
}
}
else if (touch.phase == TouchPhase.Moved)
{
// Move the trail
if (_trails.ContainsKey(i))
{
var trail = _trails*;*
Camera.main.ScreenToWorldPoint(touch.position);
var position = Camera.main.ScreenToWorldPoint(touch.position);
position.z = 0; // Make sure the trail is visible
trail.transform.position = position;
}
}
else if (touch.phase == TouchPhase.Ended)
{
// Clear known trails
if (_trails.ContainsKey(i))
{
var trail = _trails*;*
// Let the trail fade out
Destroy(trail, trail.GetComponent().time);
_trails.Remove(i);
}
}
}
}
///
/// Create a new trail at the given position
///
///
///
public GameObject MakeTrail(Vector3 position)
{
var trail = Instantiate(_trailPrefab);
trail.transform.position = position;
return trail;
}
}
_*