- Home /
using Transform on a Object will not move it 100% exactly...
Im pretty new to unity, but this thing has been bugging me for a bit.
if(posZ < max){
transform.position += new Vector3 (0, 0, Time.deltaTime * cubeMoveSpeed);
}
say the max is .. 3, what will happen is that the object will move smoothly to 3.013413241 ..
So while it is at 3.0 or 3.1, within that range anyway, it will not exactly stop at 3.00000
.. so is there anyway to fix this?
I was thinking you could potentially check if the object is in very close proximity to a number and then just place it exactly there but.. that sounds like unnecessary work, but who am I to know.. im just getting started learning.
Answer by MrSoad · Dec 03, 2014 at 06:13 PM
You are using Time.deltaTime, which you should, this though is a float value and will introduce a small amount of in-precision as a result. You need to put an "else" condition following on from your initial "if" which will then set the position exactly when the posZ is not < max. Better still check if the new position is going to be more than "max" before you move the position, then(if it is) you move exactly to the point(3.0).
if(posZ < max){
transform.position += new Vector3 (0, 0, Time.deltaTime * cube$$anonymous$$oveSpeed);
} else {
transform.position = new Vector3 (transform.position.x, transform.position.y, max);
}
There are other way to approach this scenario too, such as $$anonymous$$athf.Clamp
Also note that it is impossible to get an exact position on a Vector3, due to floating point precision limitations. Normally does not matter, but its something to be aware of.
Your answer
Follow this Question
Related Questions
Opening a door at proximity 2 Answers
A node in a childnode? 1 Answer
How to diappear a line renderer from start and end position of line 1 Answer