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 /
avatar image
0
Question by Raja-Unity · Mar 16, 2017 at 08:33 PM · scripting problempositioninspectorissueworldspace

Player's position in inspector isn't real position

I'm making a tile based movement game and I'm using Vector3.MoveTowards() to move the player. When I play my game, whenever the player's x or z position are supposed to be 0, they become random numbers in the inspector that are way off with lots of digits after the decimal. My script for the player isn't acting how it's supposed to be when it hits walls when their position is supposed 0 for x or z.

My script detects collision by looping through all the obstacles and checking if the position of the obstacle is the same as what position the player's going to go to.

Is this a glitch? Is there any way to fix this?

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

Answer by bobisgod234 · Mar 17, 2017 at 02:25 AM

Sounds like your player's position is "not quite" zero. Due to the way floating point numbers work, this can happen for various reasons when performing arithmetic on floats. You should check for collisions by using the distance between the objects positions with some tolerance.

 if (Vector3.Distance(playerPos, otherPos) < 0.1f)
 {
     // close enough to consider overlapping
 }
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 Psycho8Vegemite · Mar 17, 2017 at 08:01 AM

What you may be talking about is scientific notations, they are used either when a value is extremely small or extremely large. This is common with floats and so it's recommended to instead of checking if a float is 100% equal to another float, see if it is approximately equal. Unity's transform uses Vector3's which are just 3 floats really.

I'm not too sure if Vector3 has an approximation method so here is one that you can put into your script:

 bool Vector3Approximate(Vector3 a, Vector3 b)
 {
     if (Mathf.Approximately(a.x, b.x) && Mathf.Approximately(a.y, b.y) && Mathf.Approximately(a.z, b.z)) return true;
     return false;
 }

If the vectors you enter are roughly the same, it'll return true, otherwise false. So you can test by entering:

 Vector3 potetialCollider = /*the tiles position you're testing*/;
 Vector3 futurePlayerPos = /*the future player pos*/;
 if (Vector3Approximate(futurePlayerPos, potetialCollider))
 {
        print("Detected Collision");
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make UI appear in camera scene view? 0 Answers

Custom Editor - Show variable if bool is true in class list 1 Answer

Unity issue when displayed on 2 screens (OS X) 0 Answers

How to draw the default read-only object label in a CustomEditor? 2 Answers

How can i change position of my character 0 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