- Home /
Cant convert double to float?
hi im new to c# and converting some old code from .js and ive got this issue i cant seam to figure out. its showing the error that i cant convert double to float for the ".25 * Time.deltaTime". if i cant use ".25" is there another way i should be writing this?
void Knockback()
{
aa = transform.position;
bb = transform.position;
while (bb.y <= (aa.y + 4))
{
transform.Translate(0, (.25 * Time.deltaTime), (-.25 * Time.deltaTime));
bb = transform.position;
}
if (bb.y >= (aa.y + 4))
{
KNOCKBACK = false;
}
}
void Knockbackdamage()
{
aa = transform.position;
bb = transform.position;
while (bb.y <= (aa.y + 4))
{
transform.Translate(0, (.25 * Time.deltaTime), (-.25 * Time.deltaTime));
bb = transform.position;
}
if (bb.y >= (aa.y + 4))
{
KNOCKBACKdamage = false;
// HealthControl.DAMAGE = true;
}
}
Answer by Bunny83 · Mar 17, 2017 at 02:56 AM
A numeric literal like .25
or 0.25
does equal to 0.25d
in C#. So by default any fractional literal is treated as double. In UnityScript they made "float" the default. So in UnityScript .25
or 0.25
equals 0.25f
. In both languages you can use the "f" or "d" suffix to indicate the type of the literal.
Another way would be to cast your double value into a float by doing (float)0.25
. However this wouldn't make any sense. You usually just use 0.25f
or .25f
In general the compiler implicitly casts int to float and float to double as double has a higher precision. The reverse is not implicit but requires an explicit cast.
Thank you very much, lol i feel kinda silly now but that was very helpful.