Hello everyone im currently having trouble on making this script and it works fine but when i drag in one direction it goes in a random location and i want it to go the way im pointing to heres my script and to make the line look better a little and also how to slow down gameplay when dragging to shoot
using UnityEngine;
public class ShootBall : MonoBehaviour
{
private LineRenderer _lineRenderer;
public void Start()
{
_lineRenderer = gameObject.AddComponent();
_lineRenderer.SetWidth(0.2f, 0.2f);
_lineRenderer.enabled = false;
}
private Vector3 _initialPosition;
private Vector3 _currentPosition;
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
_initialPosition = GetCurrentMousePosition().GetValueOrDefault();
_lineRenderer.SetPosition(0, _initialPosition);
_lineRenderer.SetVertexCount(1);
_lineRenderer.enabled = true;
}
else if (Input.GetMouseButton(0))
{
_currentPosition = GetCurrentMousePosition().GetValueOrDefault();
_lineRenderer.SetVertexCount(2);
_lineRenderer.SetPosition(1, _currentPosition);
}
else if (Input.GetMouseButtonUp(0))
{
_lineRenderer.enabled = false;
var releasePosition = GetCurrentMousePosition().GetValueOrDefault();
var direction = releasePosition - _initialPosition;
Debug.Log("Process direction " + direction);
}
}
private Vector3? GetCurrentMousePosition()
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var plane = new Plane(Vector3.forward, Vector3.zero);
float rayDistance;
if (plane.Raycast(ray, out rayDistance))
{
return ray.GetPoint(rayDistance);
}
return null;
}
}