• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lampshade · May 21, 2010 at 11:55 PM · gameobjectbounds

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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 .

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lampshade · May 22, 2010 at 01:23 AM 0
Share

Would I still need to use prefabs with this? Could I just look for the name?:

void OnTriggerEnter(Collider other) {other.gameObject.tag = "Player"; }

avatar image qJake · May 22, 2010 at 07:43 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

"Mesh 'NormalCurveRendererMesh': abnormal mesh bounds..." error appears on editor... 0 Answers

Bounds Finding Box 1 Answer

Issue using separate GameObject to detect bounds 0 Answers

How should one resize a gameobject? 1 Answer

Bounds is 0 when Instantiated 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges