Translate to C#

Hello everybody

Please i want to understant why when i make this sentance with js

horMovement = Input.GetAxis("Horizontal");
	if (horMovement) {
		transform.Translate(transform.right * horMovement);
	}

it doesn’t work with c#, the error is that it cannot convert float to bool what can i do to make it work with c#?

thank you

You need to declare horMovement as a float.

i.e

float horMovement = Input.GetAxis("Horizontal");

There is also some online JavaScript to C# converters for Unity that can help you.

In C# an int or float value of 0 or 0.0 is not implicitly casted to "false" like in other languages. You have to test is manually for inequality like this:

if (horMovement != 0.0f)

On the other hand this if statement is kinda useless. Ok it doesn't call the Translate fucntion, but the result is the same. If horMovement is 0.0f Translate will be called with the zero-vector which results in "no movement".