SqrMagnitude intermittently doesn't work for if statement on prefab. (C sharp)
Hi,
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.
Adam
Answer by jdean300 · Feb 20, 2017 at 09:29 PM
Why are you using`HarpoonPos.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.
Hi @JDean300, I'll give that a shot, thank you for such a quick reply. :)
Hi @JDean300, sadly the issue is still persisting. I've changed the code to this:
//Comparing harpoon position against the destination.
if ((harpoonpos.position - PlayerTempPos).sqr$$anonymous$$agnitude <= 0.1f) {
Thanks for your help, I think you're right that it's my checking process at fault, and it's strange that it's so intermittent.
UPDATE: I've increased the constant to 20f, and now can't reproduce the issue! Thank you so much, I'll mark it as solved! :)
Your answer
Follow this Question
Related Questions
Add changeable conditions in the editor, similar to unityEvent? 0 Answers
If statement for direction the character is facing 0 Answers
The If statement condition is false but the if statement stills executes 1 Answer
Help with rotation please 2 Answers
Unity Update is ignoring the GetKey part of my statement!? 2 Answers