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 /
This question was closed Jul 17, 2017 at 02:00 AM by meistermayo for the following reason:

Other

avatar image
0
Question by meistermayo · Feb 02, 2016 at 03:04 PM · physicsfloatdatanumbersfloatingpoint

Transform ignores given numbers, creates random floats.

Hi, I'm very new to Unity. While trying to create a simple fighting game framework, I came across an odd problem. I attempted to control the movement of the basic sphere character, defining gravity and using a few float variables. Unity can move the sphere using these variables, but it seems to mess up all of the numbers.

The image below shows when the character lands on 'float groundY' which is 0.5f. It doesn't go straight to 0.5, it stays at a very ugly decimal number.

alt text

Do I have to change the logic to account for things like this or is there a way to keep Unity from doing this?

Here is the sample code

     private float hMove;
     private float vMove;
     private float walkSpeed;
     private float jumpHeight;
     private float hsp;
     private float vsp;
     private float groundY;
     private float grav;
     // Use this for initialization
     void Start () {
         walkSpeed = 0.1f;
         jumpHeight = 1.0f;
         groundY = 0.5f;
         grav = 0.0f;
     }
     
     // Update is called once per frame
     void Update () {
 //Controls, y position variable
         hMove = Input.GetAxisRaw ("Horizontal") * walkSpeed;
         vMove = Input.GetKey(KeyCode.UpArrow) ? 1 : 0;
         float thisY = transform.position.y;
 
         hsp = hMove;
 //if y position is above 0.5, decrease vertical speed by gravity.
         if (thisY > groundY) {
             grav = -0.1f;
             vsp += grav;
         } else {
             grav = 0.0f;
             vsp = vMove * jumpHeight;
         }
 //if below 0.5, vertical speed = 0, y position = 0.5;
         if (thisY < groundY) {
             vsp = 0.0f;
             thisY = Mathf.Round (groundY);
         }
 //update position
         transform.position = new Vector3 (transform.position.x + hsp, transform.position.y + vsp, transform.position.z);
     }
screen-shot-2016-02-02-at-85530-am.png (12.0 kB)
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

  • Sort: 
avatar image
0

Answer by Erethan · Feb 15, 2016 at 02:34 PM

if your object's y is 0.500000001 then you will apply gravity and It might force your object to go below 0.5f.

I would not recomend you to inquiry for that level of precision. Instead of processing your data to fit the 0.5f mark, you should allow a certain range of tolerance. This will not allow the script to add gravity when your object is close to the ground, and it consider values like 0.500000001 as if it was on ground.

Considering that you are very new to Unity:

Are you familiar with Unity Physics? Look for Physics Raycast (to groundCheck) and Rigidbodies. If you change directly into the Transform, it will not look for collisions when you are trying to move your object.

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

Follow this Question

Answers Answers and Comments

54 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

Related Questions

How to make a number higher than Quintillion? (1000000000000000000) 1 Answer

Float with Scientific Notation breaks simple addition? 1 Answer

What is the difference between float and integer (C#) 3 Answers

Character Controller too lag and no and without equal height 0 Answers

How to predict where the soccer ball should be intercepted by an AI player after being kicked? 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