Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do I make a gameobject snapped to my path in edit mode?

Discussion in '2D' started by YanivAvrahami, Mar 18, 2017.

  1. YanivAvrahami

    YanivAvrahami

    Joined:
    Jun 26, 2016
    Posts:
    51
    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.
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    I quick and easy way I do this stuff is to hijack the OnDrawGizmos() function.
     
  3. YanivAvrahami

    YanivAvrahami

    Joined:
    Jun 26, 2016
    Posts:
    51
    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?
     
  4. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    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?
     
  5. YanivAvrahami

    YanivAvrahami

    Joined:
    Jun 26, 2016
    Posts:
    51
    Getting the editor to run the code to snap the object to the correct location BUT in edit mode.
     
  6. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    JoramR likes this.
  7. YanivAvrahami

    YanivAvrahami

    Joined:
    Jun 26, 2016
    Posts:
    51
    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?
     
  8. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    That's beyond my capabilities. The above solution is good enough for me so I've never tried to find something better.