Math...RUN! Jumpiness and Inaccuracy Moving Along Circle Boundary

I have a 2d game with a slingshot firing system, similar to Angry Birds, where I drag the projectile back with the mouse and release to fire. I’ve got code to attempt to prevent the projectile from being dragged all over the screen and should keep it within the “drawrange” of “other” on the closest point toward the mouse. Note that “other” is my projectile’s starting point and “drawrange” is the maximum range I would like the projectile drawn.

	if(clicked){
		var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		if (Physics.Raycast (ray, hit)){
				mousePosX = hit.point.x; //world X coord that the hit took place at
				mousePosY = hit.point.y; //world Z coord that the hit took place at
			if(Vector3.Distance(other.position, transform.position) < drawrange){
				transform.position = Vector3(mousePosX, mousePosY, 0);
			}else{
				dist = Vector3.Distance(other.position, transform.position);
				var holderX = dist/drawrange*(other.transform.position.x - mousePosX) + other.transform.position.x;
				var holderY = Mathf.Sqrt((drawrange * drawrange) + ((other.transform.position.x-holderX)*(other.transform.position.x-holderX)));
				Debug.Log("position =" + holderX + ", " + holderY);
				transform.position = Vector3(holderX, holderY, 0);
			}
		}
	}

The issue I’m having is a significant amount of flickering of the projectile as well as motion that is not consistent with where my mouse cursor is. The debug log has a consistent number, but maybe the math routine is taking too long to complete?

I’ve double checked this for math errors but can’t find any. Admittedly my geometry is rusty though. I’m going to attempt to explain my math just in case someone is trying to follow my logic here and it doesn’t make sense.
MATH STUFF:
See attachement

This untested pseudocode should keep mouseposition within drawrange from origin.

originToMouse = mouseposition - origin;
if(originToMouse.magnitude > drawrange){
    mousposition = originToMouse.normalized * drawrange;
}

Then you can use mouseposition to place the projectile.

For performance optimization, you can check sqrMagnitude vs a precomputed sqrDrawrange, which saves a sqrt.

I don’t really understand what you are attempting to do here but I can tell you a couple of things.

  1. By no means would this routine take enough time to have any noticable impact… Nor would it cause the sort of problem you describe it if took a full minute to calculate, because Unity would wait for the code to execute before going on to other things.

  2. If your Debug.Log statement is giving expected results then my guess would be that your Physics.Raycast just isn’t hitting things very often, as a result transform.position is updated inconsistently. Physics.Raycast only returns true if the ray hit something that has a collider on it.

Thanks a ton Jasper. I’ll try that out.

I wouldnt use Physics.Raycast at all. I would use a Plane.Raycast. You simply define a plane wich is centered on your slingshot. This gives you a hit point which is where you want to be. (hitpoint=ray.origin + ray.direction.Normalize() * hitDistance)

As far as then stopping the mouse, you could easily say. If(Mathf.Abs((slingShot.position.x-hitpoint.x)<20)… and the y<10)do a raycast… Otherwise, it doens’t update the mouse position.

Then it is as simple as setting the velocity of the object and letting PhysX do the rest