1

In order to move my player immediately at constant speed, I wanted to change the user's joystick input like that:

float fH = Input.GetAxis("Horizontal"); 
fH = Math.Sign(fH);

transform.position += transform.right * fH * speed * Time.deltaTime;

Now Unity tells me that the name "Math" does not exist in the current context.

I never expected that I would have to write something like "using Math" (untested).

This made me think if I should use any NET framework class at all or just use Unity's built-in functions.

Is there a reason NOT to use the NET framework in a Unity script?

1 Answer 1

2

I believe you need to either add using System; or use System.Math.Sign(fH); - No, there is no reason you cannot use these in Unity.

However, given that you are using Unity, you might be intested in using Mathf instead. The build in Math class from .NET works with double, while Mathf is optimized to work with float. That means you would use Mathf.Sign. Errata: Reading the documentation on Mathf.Sign, it appears it will return 1 when the input is 0.

Sign up to request clarification or add additional context in comments.

1 Comment

You're right. Mathf wouldn't work, but using System; and Math.Sign does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.