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 /
This question was closed Jan 18, 2020 at 09:04 AM by andranto for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by andranto · Jan 17, 2020 at 11:09 PM · unity 5rotationpositiondrawgizmos

Draw 4 points around object

Hi all,

I want to draw 4 points around my object. I use these point as destination of my nav mesh agent.

I use this script to draw them (with Gizmos):

     private void InitWaypionts()
     {
         Vector3 size = this.gameObject.GetComponent<Renderer>().bounds.size;
         Vector3 position = this.transform.position;
 
         actionPoints = new Vector3[4];
 
         actionPoints[0] = new Vector3(position.x - (size.x / 2) - 0.2f, position.y, position.z);
         actionPoints[1] = new Vector3(position.x + (size.x / 2) + 0.2f, position.y, position.z);
         actionPoints[2] = new Vector3(position.x, position.y, position.z - (size.z / 2) - 0.2f);
         actionPoints[3] = new Vector3(position.x, position.y, position.z + (size.z / 2) + 0.2f);
     }
 
     void OnDrawGizmos()
     {
         if (actionPoints != null && actionPoints.Length > 1)
         {
             Gizmos.color = Color.yellow;
             Gizmos.DrawSphere(actionPoints[0], 0.1f);
             Gizmos.color = Color.blue;
             Gizmos.DrawSphere(actionPoints[1], 0.1f);
             Gizmos.color = Color.red;
             Gizmos.DrawSphere(actionPoints[2], 0.1f);
             Gizmos.color = Color.green;
             Gizmos.DrawSphere(actionPoints[3], 0.1f);
         }
     }

The result is: alt text

I have a problem if the object has a rotation, in this case the points are drawn in wrong position: alt text

How I can apply the rotation of these points, to draw them correctly?

screenshot-2020-01-17-at-235948.png (89.1 kB)
screenshot-2020-01-18-at-000535.png (83.7 kB)
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

  • Sort: 
avatar image
1
Best Answer

Answer by unity_ek98vnTRplGj8Q · Jan 17, 2020 at 11:20 PM

Use your transform's direction vectors -> transform.forward and transform.right


Also be careful using renderer.bounds. The size will vary as you rotate your object. You probably want to use mesh bounds instead.

  private void InitWaypionts()
      {
          Vector3 size = this.gameObject.GetComponent<MeshFilter>().sharedMesh.bounds.size;
          Vector3 position = this.transform.position;
  
          actionPoints = new Vector3[4];
  
          actionPoints[0] = position + transform.right * ((size.x/2) + 0.2f);
          actionPoints[1] = position + -1*transform.right * ((size.x/2) + 0.2f);
          actionPoints[2] = position + transform.forward * ((size.x/2) + 0.2f);
          actionPoints[3] = position + -1*transform.forward * ((size.x/2) + 0.2f);
      }

Edited to change mesh to sharedMesh

Comment
Add comment · Show 1 · 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 Bunny83 · Jan 18, 2020 at 01:14 AM 0
Share

Note that you should avoid using the mesh property as it will cause an instantiation of the used $$anonymous$$esh for this $$anonymous$$eshFilter. If you just want to read information from the mesh, use the shared$$anonymous$$esh property. Also keep in $$anonymous$$d that the instantiated $$anonymous$$esh is not automatically destroyed when the object is destroyed. It will be destroyed when Resources.UnloadUnusedAssets is called but in general you are responsible for destroying that instance when you don't need it anymore.


Also note that it's not clear when "InitWaypionts" is actually called. If it's just called once at the start / loading of the object any changes to the rotation afterwards would of course have no effect on the positions.


ps: "InitWaypionts" looks like a typo in the OP and should be refactored.

Follow this Question

Answers Answers and Comments

256 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 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 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 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

Accurate placed object's transform messes up on game start 1 Answer

How to get Objects, Rotation? 1 Answer

[Unity 3D] How to detect if the game object is left right from the player? 0 Answers

Position and rotate 3 sprites perpendicular to the edges of a triangle 1 Answer

How to make rotation return to starts after key release? #c 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