Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 HuskyPanda213 · Feb 20, 2014 at 03:14 PM · transformrounding

Rounding not working?

I have made a first person rounding system for building and its not really working... The object does not round both position and rotation will randomly make the object go to a weird transform. I do not know what is wrong. I will tell you what I want it to be like though, that a objects position is rounded to the nearest 0.25, and the rotation rounded to the nearest 30 degrees(mainly the y axis). The setup I currently use is: there is a empty object in the players First person transform(0,0,0 local pos), which is not connected to the main camera, then another object which is a child of the empty gameobject, and this one has the rounding script, then as a child of that are all of the buildables. The path for a buildable would be this "Firstperson/BuildableObjects/Rounding/NameOfBuildable".

The script I am currently using:

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class Rounding : MonoBehaviour {
 
 
     void Update(){
         RoundTrans ();
     }
 
     private void RoundTrans(){
         Vector3 gPos = transform.position;
         gPos.x = (float)Math.Round(gPos.x, MidpointRounding.AwayFromZero) / 4;
         gPos.y = (float)Math.Round(gPos.y, MidpointRounding.AwayFromZero) / 4;
         gPos.z = (float)Math.Round(gPos.z, MidpointRounding.AwayFromZero) / 4;
         transform.position = gPos;
         Vector3 gRot = transform.eulerAngles;
         gRot.y = Mathf.Round (gRot.y / 30) * 30;
         gRot.z = Mathf.Round (gRot.z / 30) * 30;
         transform.eulerAngles = gRot;
     }
 }

EDIT: Still does not work, Ive changed the code to what you said, and the rotation for somereason does not round(well a little but only under certain conditions), and the position makes the object go in front the player, then I turn it then clips into him, and then locks and freezes.

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 robertbu · Feb 20, 2014 at 03:56 PM

To round to the nearest .25, multiple by 4 before rounding, then divide by 4 after rounding.

 val = Mathf.Round(val * 4.0f) / 4.0f;

To get to the nearest 30 degrees, divide by 30, round, then multiple by 30:

 val = Mathf.Round(val / 30.0f) * 30.0f);

Note that Transform.eulerAngles can change representation, so your code may not get you what you want without more work.

Comment
Add comment · Show 3 · 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 HuskyPanda213 · Feb 20, 2014 at 05:50 PM 0
Share

Still does not work, Ive edited the question.

avatar image robertbu · Feb 20, 2014 at 06:02 PM 0
Share

There is the question you ask, and then there is the behavior you want. First, figure out if the calculations I outlined are producing the specified values. So on between lines 16 and 17, insert:

  Debug.Log(transform.position+", "+gPos);

See if the position is being rounded to the nearest 0.25.

Insert between lines 20 and 21:

 Debug.Log(transform.eulerAngles+", "+gRot);

Verify that the rotation of y and z are rounded to the nearest 30 degrees.

If they are not, post the code as you have modified it, and I'll take a look.


But there is a deeper potential issue. I don't know if the issue is what is causing your issues, and without more information or more code, I cannot give you specific on how to fix it. 'localEulerAngles' is read from the Quaternion. There are multiple euler angle representations for any given physical rotation. The angle you put in to your localEulerAngle may not be the same one you get out. So your rounding code may not be doing the correct action for a given representation.

avatar image HuskyPanda213 · Feb 20, 2014 at 07:51 PM 0
Share

Its rounding(to nearest 30 degrees, and .25), but not working as I would want it. I would like it that it rounds nearest .25 infront of the player, and to the left and right of the player, and the rotation too keep the object infront of the player(right infront), and just rotate every 30 degrees so it is easy to build.

Just saying if you noticed the buildables have not a 0,0,0 rotation or rotation i tried making other objects(the holder type objects), have the offset, and nothing changed.

I aalt texttatch screenshots.alt text

capture.jpg (174.1 kB)
capture2.jpg (205.9 kB)
avatar image
0

Answer by Gnometech · Feb 20, 2014 at 03:57 PM

Well... in your script you are rounding your position and THEN divide it by 4...?

Don't you want to do something like

 gPos.x = (float)Math.Round(gPos.x * 4, MidpointRounding.AwayFromZero) / 4;
 gPos.y = (float)Math.Round(gPos.y * 4, MidpointRounding.AwayFromZero) / 4;
 gPos.z = (float)Math.Round(gPos.z * 4, MidpointRounding.AwayFromZero) / 4;

?

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

19 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

Related Questions

Advice on reducing rounding errors with transform 1 Answer

Rounding transform.position? With parents 0 Answers

What is equivalent to Transform.up when moving and rotating the RigidBody2D component instead? 1 Answer

Calculating translation speed? 1 Answer

Convert from Image plane to World plane. Inverse perspective Mapping 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