- Home /
Access GameObject type's Dimensions
Hi,
I have a plane (gameObject) situated at 0,0,0 in the world. So far, I have accessed its position in the if condition below, and tested to see if my players position (playerPosition) is within the coordinates of the plane.
void Update() {
// Amount to Move
float MoveForward = Input.GetAxis("Vertical") * MoveSpeed * Time.deltaTime;
float MoveRotate = Input.GetAxis("Horizontal") * RotateSpeed * Time.deltaTime;
// Move the player
transform.Translate(Vector3.forward * MoveForward);
transform.Rotate(Vector3.up * MoveRotate);
// given a y-up based system, find if the player is within the game objects dimensions
if (gameObject.transform.position.x <= playerPosition.transform.position.x &&
gameObject.transform.position.z <= playerPosition.transform.position.z)
{
print("Inside");
}
else
{
print("Outside");
}
}
But that is not as consistent as I'd like it to be. I print outside when I am still inside, and inside when I am outside, sometimes.
A possible solution might be, to gain access to the dimensions of the game object. This is what I am trying to acheive, and could possibly be done if I can gain access to the dimensions of the game object.
As always, any help is very much appreciated.
Answer by qJake · May 22, 2010 at 12:55 AM
I think you're making this WAY more complicated than it needs to be. Don't try to calculate your object bounds manually, as you'll create unnecessary overhead. Leave this type of stuff to Unity, since it does it more efficiently. Instead, do something simple like this:
Add a collider to your object, and set "Is Tigger?" equal to true (Check the box).
Then, with code, you can do this:
void OnTriggerEnter(Collider other) { // Something has just entered the object here. }
void OnTriggerStay(Collider other) { // Something is currently inside the trigger here. // Called EVERY frame for each thing that is touching the trigger, // just like Update(); }
void OnTriggerExit(Collider other) { // Something has just left the trigger. }
You can get the game object using other.gameObject
.
Would I still need to use prefabs with this? Could I just look for the name?:
void OnTriggerEnter(Collider other) {other.gameObject.tag = "Player"; }
Prefabs have little to do with collisions... I don't understand what you mean. And yes, that's how you access the tag of the other object that collided (or is touching) this one.