- Home /
Returning position of game object in c#
I need to return the x position of a game object in c#, in an if statement. Basically, if object position x = something, then do something. How would I go about this. I have tried the manual, couldn't make sense of it. I am a javascript person. Thanks, Hugh
Answer by aldonaletto · Jan 24, 2013 at 10:48 PM
It's the same as in JS:
if (someObject.transform.position.x == someX){
// do something
}
But this is a bad idea in JS, C# or Boo: x is a float, which has limited precision (about 6 or 7 digits), and hardly will be equal to another float (unless both are whole numbers). Whenever possible, use >= or Mathf.Approximately, like this:
if (Mathf.Approximately(someObject.transform.position.x, someX)){
// do something
}
$$anonymous$$y script doesn't seem to know about my object...
Answer by cdrandin · Jan 24, 2013 at 10:43 PM
um ok no sure what you want, but I will make something up.
if( object.position.x > transform.position.x )
{
Debug.Log("object is to the right of this " + transform.name);
}
Assuming positive x axis is pointing to the right of the screen.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
moving random objects to random positions 1 Answer
Check if an object is changing position C# 3 Answers
Multiple Cars not working 1 Answer
Changing player's moving direction 0 Answers