I am making a 2D game in Unity where there are pucks (circles) and you have to throw them to the other side (i think this game is called Pucket or sth). However, to throw the puck to the other side you have to use a slingshot. Since this is a 2D game, it needs to be a line slingshot where you have to pull with your finger (mobile game) and depending on how much you pull, it will apply force to the puck to throw it.
How can I make this using a LineRenderer or something? (and optionally how to implement drag and drop spot for the slingshot?)
I can help a bit with the line renderer, but I don’t have any experience with touch controls.
A simple approach is to use a line renderer with 2 segments (3 points). You can set it up like this:
//Empty game objects that we are just using to specify positions for the anchors
[SerializeField] private GameObject anchor0;
[SerializeField] private GameObject anchor1;
private void Awake()
{
LineRenderer myLine = GetComponent<LineRenderer>();
Vector2 midpoint = (anchor0.transform.position + anchor1.transform.position) / 2;
myLine.positionCount = 3;
myLine.SetPosition(0, anchor0.transform.position);
myLine.SetPosition(1, midpoint);
myLine.SetPosition(2, anchor1.transform.position);
}
Then when you want to click and drag the midpoint, you just call SetPosition with index 1 again. You’ll likely want to implement some bounds limit so that the user can’t drag the midpoint anywhere. You can use the distance between the current drag location and the original midpoint location to determine the force of ths shot.