I am trying to make Line Renderer's first point move along with Camera. When I do it with a Camera that is still, it works fine. But when camera changes position and in that position if I try to create new Line with Line Renderer it goes weird. I tried updating the position of first point by multiplying camera.tranform.position with dragBeginPos but it doesn't seem to help as it makes it even bad.
public class DragController : MonoBehaviour
{
[SerializeField] private float maxDrag = 3f; // Maximum drag distance
[SerializeField] private LineRenderer lr;
private Vector3 dragBeganPos; // Position of the mouse or touch when the drag began
private Vector3 dragEndPosPublic;
private Touch touch;
private PlayerBallMovement playerBallMovement;
[HideInInspector] public Vector3 dragDirection; // Pure drag direction without max drag distance
[HideInInspector] public Vector3 dragDirectionClamped; // Clamped drag direction with max drag distance
private enum ControlsType
{
Touch,
Mouse
}
private enum DirectionType
{
Normal,
Clamped
}
[SerializeField] private ControlsType controlsType = ControlsType.Mouse; // Default controls type are mouse
[SerializeField] private DirectionType directionType = DirectionType.Clamped; // Default direction type is clamped
void Update()
{
if (controlsType == ControlsType.Mouse)
{
DragWithMouse();
}
else if (controlsType == ControlsType.Touch)
{
DragWithTouch();
}
//UpdateLineRendererPointsPositions();
}
void DragWithMouse()
{
// Mouse button inputs
if (Input.GetMouseButtonDown(0)) DragBegan();
if (Input.GetMouseButton(0)) DragMoved();
if (Input.GetMouseButtonUp(0)) DragEnded();
}
void DragWithTouch()
{
// Touch inputs
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began) DragBegan();
if (touch.phase == TouchPhase.Moved) DragMoved();
if (touch.phase == TouchPhase.Ended) DragEnded();
}
}
// Drag begins defining start position of Line Rederer
void DragBegan()
{
dragBeganPos = Camera.main.ScreenToWorldPoint(
controlsType == ControlsType.Mouse ? Input.mousePosition : (Vector3)touch.position
);
dragBeganPos.z = 0f;
lr.positionCount = 1;
lr.SetPosition(0, dragBeganPos);
}
// Drag moved calculates the end point of the Line Rederer until mouse or touch is released
void DragMoved()
{
Vector3 dragMovedPos = Camera.main.ScreenToWorldPoint(
controlsType == ControlsType.Mouse ? Input.mousePosition : (Vector3)touch.position
);
dragMovedPos.z = 0f;
lr.positionCount = 2;
lr.SetPosition(1, dragMovedPos);
}
// Drag ended defines the end point of the Line Rederer and calculates the drag direction and clamped drag direction
void DragEnded()
{
lr.positionCount = 0;
Vector3 dragEndedPos = Camera.main.ScreenToWorldPoint(
controlsType == ControlsType.Mouse ? Input.mousePosition : (Vector3)touch.position
);
dragEndedPos.z = 0f;
dragEndPosPublic = dragEndedPos;
dragDirection = dragEndedPos - dragBeganPos; // It will apply force without any max speed defined (no clamp)
dragDirectionClamped = Vector3.ClampMagnitude(dragDirection, maxDrag); // It will apply force with max speed defined
Vector3 finalDirection = directionType == DirectionType.Clamped ? dragDirectionClamped : dragDirection;
// Here you can use dragDirection or dragDirectionClamped however you want
playerBallMovement = FindObjectOfType<PlayerBallMovement>();
playerBallMovement.ApplyVelocityInDirection(finalDirection);
}
}```