Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by egonspengler_84 · May 03, 2021 at 03:09 AM · rigidbody2dcollision detectionboundskinematicboundingbox

Issue with checking to see whether the Bounds value's of two game objects equal each other

Hi there,

So basically I have two game objects in a scene - one is just a rectangle shaped game object and the other is a wall. Both game object have BoxCollider2D components attached to them. The rectangle has a Dynamic Rigidbody2D attached to it.

Basically I have code that makes the rectangle travel horizontally to the right towards the wall. I have an if statement that states when the X value of the Bounds.Max position of the rectangle touches the X value of the Bounds.Min position of the wall a message should pop up in the console.

Here is the code that I have:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class PlayerNew : MonoBehaviour
 {
     private Rigidbody2D _rb;
     private BoxCollider2D _col2D;
     private float X_force = 25f;
     public Vector2 newPosition;
     public Vector2 bottomRight;
    
     public Vector2 wallBoundsMin;
    
  
     // Start is called before the first frame update
     void Start()
     {
      
       _rb = GetComponent<Rigidbody2D>();  
       _col2D  = GetComponent<BoxCollider2D>();
       wallBoundsMin = GameObject.Find("Wall").GetComponent<BoxCollider2D>().bounds.min;
  
     }
  
     // Update is called once per frame
     void Update()
     {
        
         bottomRight = new Vector2(_col2D.bounds.max.x,_col2D.bounds.min.x);
        
  
  
         newPosition = new Vector2(X_force,0);
        _rb.MovePosition(_rb.position + newPosition * Time.deltaTime);
  
  
       if(bottomRight.x == wallBoundsMin.x){
         Debug.Log("Bounds have collided with one another");
       }
      
        
     }
 }
  

However nothing appears in the console when the rectangle crosses the wall. But the message actually does appear in the console if I change the if statement to the following:

 if(bottomRight.x >= wallBoundsMin.x){


I'm confused as to why the message wont appear when I just check to see when the two X value's equate to one another? I don't think I've written the code incorrectly? Here is a video of the issue: https://www.youtube.com/watch?v=J9g-kF8WR9g&ab_channel=segoviate

Any help would be appreciated. Thanks

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
0

Answer by GSGregory · May 04, 2021 at 06:22 AM

Floats are an inaccurate number. Don't compare floats with ==. For example. 0001 and .0002 are not equal to a computer. So basically when the comparison is called they are both not exactly the same value. I think there is a way to ignore a certain level of discrepancy but you are probably better off just calling > or >=

Another example is if you divide 10f by 3 you might get 3.333332, or 3.333334 ect

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

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

123 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

Related Questions

Question about raycast collision detection involving Kinematic Rigidbody2D 1 Answer

2D Colliders stops working at random despite rigidbody, OnMouseDown not called 0 Answers

Blocking a kinematic rigid body2D through collision with static and/or other kinematic object? 2 Answers

Player is only detected in trigger when moving 2 Answers

Colliders not working when rotating 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