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 /
  • Help Room /
avatar image
0
Question by Allgames88 · Feb 14 at 10:02 AM · floatmultiple

I need to move multiple objects to a position multiple of 0.16. But it doesnt work.

Im Using Unity 2D.

Okay, so i need to move an object to its nearest position multiple of 0.16, to do this i started rounding its coordinates and leaving only two decimals, after that, i was checking if the position was multiple, if not, rest 0.01f to the float, and repeat, if yes, it would stop checking and move the object to that position.

The problem is that all the objects in a positive position always end in 0,0, and all the objects in a negative position, wont move at all, the bucle just never stops, but i dont get why is it failing.

By the way, im from Spain so sorry if there is something misspelled.

Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SingleGrass : MonoBehaviour
 {
     //Variable declarations.
     public string ID;
     public int level;
     float Posx;
     float Posy;
     public float Decimaly;
     public float Decimalx;
     bool checkx = true;
     bool checky = true;
 
     // Start is called before the first frame update
     void Start()
     {
         Posx = transform.position.x;
         Posy = transform.position.y;
         Decimalx = GetDecimal(Posx);
         Decimaly = GetDecimal(Posy);
         Vector2 RoundedPosition = new Vector2(GetDecimal(Posx),GetDecimal(Posy));
         transform.position =RoundedPosition;
     }
 
     // Update is called once per frame
     void Update()
     {
         //If the position that its checking its not a multiple of 0.16, it will go and check the position - 0.01.
         if(Decimalx % 0.16 != 0 && checkx){
             Decimalx = Decimalx - 0.01f;
             Decimalx = GetDecimal(Decimalx);
         }
         
         //If the position its checking IS multiple of 0.16, it will stop checking and will give that position to the x coordinates of the object.
         if(Decimalx % 0.16 == 0){
             transform.position = new Vector2(Decimalx,GetDecimal(transform.position.y));
             checkx = false;
         }
 
         //If the position that its checking its not a multiple of 0.16, it will go and check the position - 0.01.
         if(Decimaly % 0.16 != 0 && checky){
             Decimaly = Decimaly - 0.01f;
             Decimaly = GetDecimal(Decimaly);
         }
         //If the position its checking IS multiple of 0.16, it will stop checking and will give that position to the y coordinates of the object.
         Decimaly = GetDecimal(Decimaly);
         if(Decimaly % 0.16 == 0){
             transform.position = new Vector2(GetDecimal(transform.position.x),Decimaly);
             checky = false;
                 
         }
         
 
         
     }
 
     /*======== Math Calculation Functions ========*/
     //This function will round any float, and will only left 2 decimals.
     public float GetDecimal(float b){
         float TruncatedFloat = Mathf.Round(b*100.0f)*0.01f;
         float a = /*Mathf.Round(b) - */TruncatedFloat;
         //Debug.Log(a);
         return a;
     }
 
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

175 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

Related Questions

How to set float, bool from one script to the other? 1 Answer

Void being called 4 times, no apparent reason 1 Answer

shooting multiple cannons with delay 1 Answer

Same multiple model but with different materials/textures 1 Answer

Code setting 0 as the highscore [Read description] 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