Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Adam_is_learning_all_the_time · Feb 20, 2017 at 09:01 PM · c#if-statementsifif statementdistance check

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Adam_is_learning_all_the_time · Feb 20, 2017 at 10:07 PM 0
Share

Hi @JDean300, I'll give that a shot, thank you for such a quick reply. :)

avatar image Adam_is_learning_all_the_time Adam_is_learning_all_the_time · Feb 20, 2017 at 10:17 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

315 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges