SqrMagnitude intermittently doesn't work for if statement on prefab. (C sharp)


I’m looking for some help reliably determining if a projectile reaches its intended destination. At the moment, the harpoons usually work a charm, firing, stopping and then returning to the gun to fire again, but occasionally they will stop at the destination. This means that they stay awkwardly until I manually tick the “DestinationReached” variable in the Inspector.

Here is my current code, I’m led to believe it’s the if statement about SqrMagnitude which is the issue, but I’m unsure why it doesn’t always work or not work.

		if (Activated == true) {
			if (DestinationReached == false) {

				//harpoonpos is the only child gameobject to the individual guns on a prefab.
				harpoonpos = this.gameObject.transform.GetChild (0);
				//print (harpoonpos.name);
				//print ("Playertemp in loop: " + PlayerTempPos);
				//print (harpoonpos.transform.parent);
				harpoonpos.position = Vector3.Lerp (harpoonpos.position, PlayerTempPos, 2.0f * Time.deltaTime);

			//ensures the harpoon is facing player the right way.
				harpoonpos.rotation = PlayerTempRot;
				harpoonpos.Rotate (0, 90, 0);

				//waiting started was invoked in another function, but for now it's used to ensure this code doesn't loop.

			if (waitingstarted == false)
				//Comparing harpoon position against the destination.
					if ((harpoonpos.position - PlayerTempPos).sqrMagnitude <= (harpoonpos.position * 0.10f).sqrMagnitude) {
					//print ("Destination Reached");
					DestinationReached = true;

					harpoonpos.rotation = transform.rotation;
					harpoonpos.Rotate (0, 90, 0);
					//Invoke ("waiting", 2f);

					waitingstarted = true;

		//next step is returning harpoon to the gun it fired from... all the following code always works if destinationreached variable changes- the problem seems to be with the if statement above...
			if (DestinationReached == true) {

			//return to the parent object
				harpoonpos.position = Vector3.Lerp (harpoonpos.position, transform.position, 1.0f * Time.deltaTime);
				harpoonpos.rotation = transform.rotation;
				harpoonpos.Rotate (0, 90, 0);
				//print (transform.name);

			//same function again, but this time using the gun's transform. Again, this always works too?
				if ((harpoonpos.position - transform.position).sqrMagnitude <= (harpoonpos.position * 0.01f).sqrMagnitude) {
			print ("back in the gun");
					Source.Stop ();
					waitingstarted = false;
					DestinationReached = false;
					Activated = false;

This is the only code interacting with the harpoons besides one on the harpoon’s collider which detects collisions with the Player tagged colliders.

I’ve notices the issue only happens if the player is to the left of the guns (and then only occasionally), and changing “DestinationReached” manually gives the desired result. The issue is more frequent if I change “DestinationReached” to private variable.

I would love to hear any suggestions or insights anyone has into getting this working more reliably, thank you for reading.


Why are you usingHarpoonPos.position * .1f To check if a harpoon has reached its target yet? This means that your tolerance for reaching the destination is based on its position values. So a harpoon at (100,100,100) has a much higher tolerance than one at (1,1,1). Try using a simple constant, like .1f.