- Home /
The question is answered, right answer was accepted
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:
I have a problem if the object has a rotation, in this case the points are drawn in wrong position:
How I can apply the rotation of these points, to draw them correctly?
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
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
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