- Home /
C# Check If Scripted Gameobject goes Past Variable Gameobject
I'm trying to create an if statement that checks if the scripted gameobject goes past the gameobject variable character. I've tried if(gameobject.transform.position
In a 3D environment, you have be real clear about definitions. What does "goes Past" mean? If you mean further from the origin, you can do:
if(transform.position.sqr$$anonymous$$agnitude <= character.transform.position.sqr$$anonymous$$agnitude)
If you mean in front of the forward of the game object you can do:
var localPos = transform.InverseTransformPoint(character.transform.position);
if (localPos.z > 0) {
Debug.Log("I'm in front of the character");
}
Or you may mean something else entirely.
Can you explain your game in more detail? Is this a linear level? What exactly do you mean by goes past?
I want to check if the scripted gameobject's transform position is less then or equal to the character gameobject's transform position. Like what I tried with my if statement. I can't check that because vector3s are structs not variables so I can't use the
Again was does 'less than' mean? You have an x, y, z component to a Vector3. If it is less than on the x and y but more on the z, is that 'less than'? There's no clear definition of 'less than' without a clear definition of what you are trying to achieve in the environment.
If you want all three components to be less than, then you can do:
var v1 = transform.position;
var v2 = character.transform.position;
if (v1.x <= v2.x && v1.y <= v2.y && v1.z <= v2.z) {
Debug.Log("I'm less than");
}
Your answer
Follow this Question
Related Questions
C# Rotate GameObjects Regardless of List Size 2 Answers
C# Change Script Help 1 Answer
C# SetActive GameObject Array 2 Answers
C# GUI.Button Transform.Position 1 Answer
C# GameObject Reverses Z Rotation 0 Answers