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 unity_user_223 · Sep 25, 2018 at 05:53 AM · c#if-statement

if statement not runing when it should (Fixed)

in my game's code this code will not work

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class pozkill : MonoBehaviour {
     [SerializeField]
     bool debug = false;
     [SerializeField]
     Vector3 poz_to_kill = new Vector3(0f,0f,0f);
     [SerializeField]
     bool show_poz = false;
     [SerializeField]
     float time_intill_delt = 0;
     float time = 0;
     // Use this for initialization
     void Start ()
     {
         
     }
     
     // Update is called once per frame
     void Update ()
     {
         if (show_poz == true)
         {
             Debug.Log("poz is: " + this.transform.position);
             show_poz = false;
         }
         if (this.transform.position == poz_to_kill)
         {
             if (debug == true)
             {
                 Debug.Log("pos is good");
             }
             if (time >= time_intill_delt)
             {
                 Destroy(this);
             }
             if (time <= time_intill_delt)
             {
                 time = time + Time.deltaTime;
                 if (debug == true)
                 {
                     Debug.Log("time = " + time + "time_intill_delt = " + time_intill_delt);
                 }
             }
         }
         else
         {
             if (debug == true)
             {
                 Debug.Log(this.transform.position + "!=" + poz_to_kill);
             }
         }
     }
 }

the 2nd if statement will not run when this.transform.position and poz_to_kill are equal

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

2 Replies

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

Answer by unity_user_223 · Sep 27, 2018 at 04:51 AM

FIx using System.Collections; using System.Collections.Generic; using UnityEngine; public class pozkill : MonoBehaviour { GameObject perent; [SerializeField] bool debug = false; [SerializeField] Vector3 poz_to_kill = new Vector3(0f,0f,0f); [SerializeField] bool show_poz = false; [SerializeField] float time_intill_delt = 0; [SerializeField] float limite = 0.2f; [SerializeField] string name = ""; float negetivlimite_help =0; float negetivlimite = 0; float time = 0; float X =0; float Y =0; float Z =0; string XYZ = ""; // Use this for initialization void Start () { negetivlimite_help = limite * 2; negetivlimite = limite - negetivlimite_help; perent = GameObject.Find(name); } // this.transform.position, poz_to_kill // Update is called once per frame void Update() { XYZ = ""; if (show_poz == true) { Debug.Log("poz is: " + this.transform.position); show_poz = false; } X = this.transform.position.x - poz_to_kill.x; if (X < limite) { if (X > negetivlimite) { X = 0; } } XYZ = XYZ + X; Y = this.transform.position.y - poz_to_kill.y; if (Y < limite) { if (Y > negetivlimite) { Y = 0; } } XYZ = XYZ + Y; Z = this.transform.position.z - poz_to_kill.z; if (Z < limite) { if (Z > negetivlimite) { Z = 0; } } XYZ = XYZ + Z; if (debug == true) { Debug.Log(XYZ); } if (XYZ == "000") { if (debug == true) { Debug.Log("pos is good"); } if (time >= time_intill_delt) { Destroy(perent); } if (time <= time_intill_delt) { time = time + Time.deltaTime; if (debug == true) { Debug.Log("time = " + time + "time_intill_delt = " + time_intill_delt); } } } else { if (debug == true) { Debug.Log(this.transform.position + "!=" + poz_to_kill); } } debug = false; } }

Comment
Add comment · 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
0

Answer by hans_ruebe · Sep 25, 2018 at 12:13 PM

Ok you should try to write a funktion that calcutlates the distance between the this.transform.position and the poz_to_kill because the (==) method seems to be flawed in this case...

Therefore you should write a function like this...

 public bool V3Equal(Vector3 a, Vector3 b)
     {
         return Vector3.SqrMagnitude(a - b) < 0.0001;
     }

and in your origianl code, use this :

 if (V3Equal(this.transform.position, poz_to_kill))
         {
             if...

https://answers.unity.com/questions/395513/vector3-comparison-efficiency-and-float-precision.html

here is a similar awnser that explains the issue further.

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 Hellium · Sep 25, 2018 at 12:35 PM 0
Share

Unity already has overloaded the == operator, so no need to create your own function.

     public const float kEpsilon = 0.00001F
     public static bool operator==(Vector3 lhs, Vector3 rhs)
     {
         return Sqr$$anonymous$$agnitude(lhs - rhs) < kEpsilon * kEpsilon;
     }
avatar image unity_user_223 Hellium · Sep 28, 2018 at 04:00 AM 0
Share

how do you make it in to a IF statement

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

151 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

Related Questions

Unity freeze when open script,I keep trying to open a script, but it freezes Unity. Please help. 1 Answer

Moving relative to camera 0 Answers

unable to use dontdestroy on load 1 Answer

Trying to get audiosource to play once 1 Answer

How do I not trigger animation during play state of the triggered animation. 1 Answer


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