Android 2D slingshot with position relative to where the user touched?
This is only my second project in Unity and my first one with touch controls so I'm a little unsure of how to move forward. I am creating a game where the player flings a character back and fort across the screen between two tethers by pulling back on him and releasing. I got it working by monitoring the users finger position and setting the character to it, with a few limitations to ensure it doesn't pull back too far or too low ect. But that required the user to move their finger back and forth to reach the character each time, where as for convinence I want them to be able to just swipe anywhere on the screen and the character to move relative to his position. I attempted to change my code to do this by recording the begin touch phase and subtracting it from the current touch position, then setting the characters position equal to that plus its original position. However this causes the character to instantly be pulled way too far back off the screen and becomes nonresponsive to any motion. they just sit there in the air until you release them. I'm not sure what's causing this behavior, any ideas of what it could be, or perhaps of a better way to accomplish what I want?
my code is as follows:
void Dragged()
{
Vector3 mousePoint = Camera.main.ScreenToWorldPoint(Input.touches[0].position);
Vector2 anchortomouse = mousePoint - anchor.transform.position;
Vector3 touchmove = mousePoint - touchOrigin;
if(touchmove.sqrMagnitude > maxstretchsqr)
{
mouseRay.direction = touchmove;
touchmove = mouseRay.GetPoint(maxstretch);
}
if(touchmove.y > anchor.position.y - .25f && ToIgnore.value == 2 )
{
touchmove.y = anchor.position.y - .25f;
}
if(touchmove.y < anchor.position.y + .25f && ToIgnore.value == 8)
{
touchmove.y = anchor.position.y + .25f;
}
if(touchmove.x > 2.4)
{
touchmove.x = 2.4f;
}
if(touchmove.x < -2.4)
{
touchmove.x = -2.4f;
}
mousePoint.z = 0;
touchmove.z = 0;
transform.position = anchor.transform.position + touchmove;
}
where touchOrigin is the original touch position recorded earlier in the code. And anchor is the position of the anchor of the springjoint the character is attached to.