- Home /
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.
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.
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.
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.
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;
?
Your answer
Follow this Question
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