- Home /
Using the Math class and the x/y/z coordinates of models.
I'm working in C# in Unity3D. I want to be able to use the Math class and the functions it has , particularly the absolute value function/Math.Abs , but when I try to import the Math class by typing: "using System.Math" it does not work and says that 'System.Math' is not a namespace.
Another issue I am having is keeping track of the coordinates of the player and the enemy models on screen. I come from an ActionScript background so normally I would simply be able to use "this.x" "objectName.x" and similar pieces of code. However, this doesn't appear to be the same for C#/Unity. Is there a way to directly access the coordinates of an object? I tried using this.x, gameObject.position.x, but that doesn't work.
Thanks.
These are two separate questions and should be in separate Questions
Answer by Eric5h5 · May 10, 2011 at 04:13 PM
Interestingly, this works with JS:
import System.Math;
print (Abs(-5));
However, you don't actually need to import anything to use it, that just makes it more convenient. This works in JS and C#:
print (System.Math.Abs(-5));
You can always just use Unity's Mathf class, though there are some things in System.Math that aren't in Mathf, and also I don't consider Mathf.Sign to have the correct behavior, and prefer to use System.Math.Sign instead.
Always thought that was goofy; much better alternative. Why does Unity bother with duplication of these functions?
Good question. At least they're in the docs, though, so people who are unaware of $$anonymous$$ono/.NET can find them.
Answer by DaveA · May 10, 2011 at 03:27 PM
transform.position.x Have you looked at the scripting reference at all? It's very helpful: http://unity3d.com/support/documentation/ScriptReference/index.html