Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by omatase · Dec 27, 2016 at 12:06 AM · vector3positioning

How to find point just outside of box collider?

I think this one is pretty easy but I couldn't find the answer on my own. I have a character, above which I want to display a game object. In this case it's a health bar. Because of some other decisions about the design of the game I cannot parent the object with the character so I can't pre-position the object above the character. I have a box collider around the character that I can use to determine the size of the character. How can I get a Vector3 point indicating a position directly above the character, just outside of the box collider?

Here is the code that I've tried to use. It's very inconsistent though and unusable. For some characters the status display is positioned well, for others it is way off. I'm obviously doing something wrong.

 BoxCollider collider = characterScript.gameObject.GetComponent<BoxCollider>();
 
 if (collider != null)
 {
     GameObject miniStatusDisplay = (GameObject)Instantiate(StatusDisplayPrefab, transform);
 
     miniStatusDisplay.transform.position = Camera.main.WorldToScreenPoint(new Vector3(collider.transform.position.x, collider.transform.position.y + collider.size.y, collider.transform.position.z));
 }

Attached is a visual representation of what I'm trying to accomplish.

alt text

Appreciate any help.

example.jpg (121.9 kB)
Comment
Add comment · Show 3
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 AlwaysSunny · Dec 27, 2016 at 01:45 AM 0
Share

You know the height of the collider and its center in 3D space. Add half the height to the position to find the top of the box. You can add more or less distance (in the up direction) to create a desirable offset.

Of course certain conditions may make this slightly more complicated, depending on exactly how you're using your gameobjects and colliders, but this is the basic idea regardless, and belongs to a family of math tricks you'll use again and again.

avatar image omatase AlwaysSunny · Dec 27, 2016 at 06:23 PM 0
Share

Thanks @AlwaysSunny, this is in fact what I'm trying to do by taking the collider.size.y but it's not working. I will spend some more time on this again. Something about how inconsistent my results are makes me think I'm missing something, like maybe the units in which the box collider is measured isn't the same as the units of the world or something like that.

avatar image AlwaysSunny omatase · Dec 27, 2016 at 09:46 PM 0
Share

This may indeed be part of the problem. You may be doing your math in the wrong "space" - depending upon factors we aren't seeing here. If you want to get a more detailed assessment, can you elaborate on your situation a bit? What happens if, for instance, you simply ignore the collider and add to the object's position to find your point?

 Vector3 myPoint = character.transform.position + ( Vector3.up * 5 );
 myPoint = Camera.main.WorldToScreenPoint( myPoint );

Oh, and be doubly sure that the prefab you're instantiating has its elements centered on its origin. That's an easy mistake to make while building prefabs - having their contents at a weird local position.

1 Reply

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

Answer by omatase · Dec 27, 2016 at 11:48 PM

Thanks so much for your help. I figured out the problem! When I scale the characters, the box collider size stayed the same, so I had to scale down the box collider height by the character scale. Now it's working correctly!

Here's the code

 var statusRectTransform = current.Value.MiniStatusDisplayScript.GetComponent<RectTransform>();
 
 BoxCollider collider = current.Value.CharacterScript.gameObject.GetComponent<BoxCollider>();
 
 if (collider != null)
 {
     var newPosition = Camera.main.WorldToScreenPoint(new Vector3(current.Value.CharacterScript.gameObject.transform.position.x, current.Value.CharacterScript.gameObject.transform.position.y + (collider.size.y * current.Value.CharacterScript.gameObject.transform.localScale.y), current.Value.CharacterScript.gameObject.transform.position.z));
 
     current.Value.MiniStatusDisplayScript.transform.position = new Vector3(newPosition.x - ((statusRectTransform.rect.width * statusRectTransform.localScale.x) / 4), newPosition.y + ((statusRectTransform.rect.height * statusRectTransform.localScale.y) / 2), newPosition.z);
 }

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

68 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help with coordinate conversion. 1 Answer

Player Positioning on a Rotating Circle 0 Answers

Having issues with offsetting an object 1 Answer

How to instantiate Object to specific location? 1 Answer

Get an array or list of points and normals of mesh surface (Not vertecis) 1 Answer


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