how to fix this

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;
}

}

Use code tags when posting code, it makes the code much more readable.

It’s most likely a problem with the GetCurrentMousePosition. At a glance, your “plane” doesn’t seem correct - what are you intending to raycast at? It currently is facing forward, which in most situations would be away from the camera, so it’s likely that your plane.Raycast is setting “rayDistance” to a negative value (as it says it does in the docs). This could cause lots of weirdness when you try and get a point along that ray.

Basically all i want it to do is when i pull backwards it launches in the opposite direction so i pull back it launches foward

It seems you are also having trouble using the code formatting. This is the fourth post you have made today while blatantly ignoring the formatting guidelines:

Please use code tags: https://discussions.unity.com/t/481379

Are we simply asking too much of you?