How to find a Position on a line given two points & a distance? [Img Example, 2D]

Hello!

2176794--144178--anglequestion.jpg

Basically I’d like to get the touch joystick controller on the screen to follow the players touch if they move out of it’s bounds. I figure I’d just, each frame, move it a bit if the finger is out of bounds. but I’m not sure how to actually find the position required.

Anyone who remembers Geometry and Trig know the answer? THANK YOU!!!

Please forgive this code – It was created with the help of someone who was much smarter than me and may be very, very ugly! :smile:

joystickTouchPos = Input.mousePosition;
screenPosJoystickM = Vector3(joystickTouchPos.x, joystickTouchPos.y, 20);
worldPosJoystickM = guiCamera.ScreenToWorldPoint(screenPosJoystickM);
joystickCenterX = JoystickObject.transform.position.x;
joystickCenterY = JoystickObject.transform.position.y;
currentTouchX = worldPosJoystickM.x;
currentTouchY = worldPosJoystickM.y;
varcenterJoystickM = Vector2(joystickCenterX, joystickCenterY);
varcurrentJoystickM = Vector2(currentTouchX, currentTouchY);
vardistJoystickM = Vector2.Distance(centerJoystickM, currentJoystickM);
if (largeScreen)
 {
distJoystickM = distJoystickM * 2;
 }
varhorJoystickM = currentTouchX - joystickCenterX;
varverJoystickM = currentTouchY - joystickCenterY;

moveX = currentTouchX - joystickCenterX;
moveY = currentTouchY - joystickCenterY;
varangleJoystickM : float = Mathf.Atan2(moveX, moveY) * Mathf.Rad2Deg;
distJoystickM = distJoystickM * 1.33;

//DoCircleMovementHere
if (distJoystickM > 4.0)
 {

 }

distJoystickM = Mathf.Clamp(distJoystickM, 0, 4);

if (GetComponent(joystick).isNotAttacking)
 {
playerObject.GetComponent(playerScript).angle = angleJoystickM + mainCamera.GetComponent(SmoothFollow).angleOffsetX;
playerObject.GetComponent(playerScript).moveSpeed = distJoystickM;
 }

obviously not real code but

normalised(touch centre - joystick centre) * radius

i think… get the direction, normalise it so it’s base 1, then go radius distance in that direction