Let’s say I have a script called CircularPath, this path is a circle shape path which has radius, center point and angle variables. The angle variable specifies where the object is on the circle circumference. Now, if I create the path object which holds the CircularPath script and I create a game object which I want it move in the path, I want the game object to be snapped to it’s rightful position already in the editor mode and not only in play mode then how do I do it?
I tried using the attribute ExecuteInEditMode but it doesn’t good for my need.
I hope someone can help me with that,
Thank you in advance.
I quick and easy way I do this stuff is to hijack the OnDrawGizmos() function.
I know how to draw the path. I’m only trying to understand how to move the game object to snap into the path while in edit mode, do you know how to do that?
What is the problem you’re having exactly? Getting the nearest path position? Or getting the editor to run the code to snap the object to the correct location?
Getting the editor to run the code to snap the object to the correct location BUT in edit mode.
Yeah, so as a quick hack I use OnDrawGizmos instead of Update and [ExecuteInEditMode] as I find that it gets called much more reliably.
So,
void OnDrawGizmos()
{
transform.position = myClosestPathPosition;
}
I admit it’s not perfect. If you want to do it better then you can make a custom editor using handles or something like that.
Related reading:
1 Like
It actually works
but the object will snap into the right position only if the scene view window is focused (only when I click on the scene window) it’s because the function OnDrawGizmos() is called only when the scene window is focused.
I wonder how would you do this with custom editor?
That’s beyond my capabilities. The above solution is good enough for me so I’ve never tried to find something better.