- Home /
Gizmos.DrawSphere with a radius not known beforehand (not a constant)?
I have a simple custom plane object with a Height and a Width properties. I want to draw a sphere, that will cover the plane object so I have to figure out if Height is greater than Width or it's the opposite to determine the radius. In other words, I'm not passing a constant - I'm passing a value, that requires some logic to be done before hand to be done first, to get the right value. I did this, and I didn't get any visuals:
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, Mathf.Max(Width, Height));
}
Of course if I explicitly tell it the radius (give it a constant) - it works.
Is there a way to do what I want? or this is just how it works? (the radius has to be a constant)
I tried sticking [ExecuteInEditMode] (doesn't make so much sense but just a try) - it didn't do anything.
Any ideas?
Thanks.
Are Width, Height and the return value from $$anonymous$$ax() all sane values?
Sorry, sane values? did you mean scene values? - in either cases, didn't get what you mean.
Answer by KellyThomas · Dec 15, 2013 at 10:29 PM
The DrawWireSphere() call works when you pass a literal value, so the issue may with the value returned by Max().
I recommend checking to see that all involved values are in the range that you are expecting i.e. a sanity check. Something like this would do.
using System;
// ....
float max = Mathf.Max(Width, Height);
Debug.Log(String.Format("Width: {0}, Height: {1}, Max:{2}", Width, Height, max));
Gizmos.DrawWireSphere(transform.position, max)
Although I'm not really convinced as to why this works, but it does... The values are fine I just had to use a float max variable that takes the return value from $$anonymous$$ax, and use the variable ins$$anonymous$$d.
I just tried to reproduce the issue but my gizmo was drawing on all occasions. Odd behaviour...
Well... I forgot to mention that Height and Width are properties. I didn't think it would make a difference, but it seems it does. I just passed in $$anonymous$$ax(x, y) where x, y are just normal variables, it worked....
I also just tried passing GetX() and GetY() as $$anonymous$$ax's parameters. Works. Very strange...
Your answer