简体   繁体   中英

Using the NET framework in a Unity script

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM