I have been facing an issue in using a Drag and Shoot launcher. I am taking the mouse position as input and converting it to world coordinates. Those world coordinates are also being used to draw a line renderer “lr” as Drag line to show aim direction and power. The problem I have is when the camera is moving. When the camera is moving, the startpoint goes out of view and dragpoint keeps on changing with camera even if the mouse position is still. I want the startpoint and dragpoint to not get affected by the camera movement and should only be the coordinates at which we have the mouse clicked and holded.
I tried
startPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition) - Camera.main.transform.position;
This solves the problem but the line renderer stops showing the line of drag.
if (Input.GetMouseButtonDown(0))
{
startPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
lr.positionCount = 1;
lr.SetPosition(0, startPoint);
Debug.Log(startPoint);
}
if (Input.GetMouseButton(0))
{
dragPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
lr.positionCount = 2;
lr.SetPosition(1, dragPoint);
Debug.Log(dragPoint);
}
if (Input.GetMouseButtonUp(0))
{
lr.positionCount = 0;
endPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//endPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition) - Camera.main.transform.position;
force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));
rb.AddForce(force * power, ForceMode2D.Impulse);
//SoundManager.instance.PlaySfx(release);
groundCheck = false;
groundCheck2 = false;
}