1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice
  3. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  4. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  5. Unity 5.5 is now released.
    Dismiss Notice
  6. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  7. Unity 5.6 beta is now available for download.
    Dismiss Notice

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

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

  1. elgauly

    elgauly

    Joined:
    Jun 26, 2016
    Posts:
    44
    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:
    595
    I quick and easy way I do this stuff is to hijack the OnDrawGizmos() function.
     
  3. elgauly

    elgauly

    Joined:
    Jun 26, 2016
    Posts:
    44
    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:
    595
    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. elgauly

    elgauly

    Joined:
    Jun 26, 2016
    Posts:
    44
    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:
    595
  7. elgauly

    elgauly

    Joined:
    Jun 26, 2016
    Posts:
    44
    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:
    595
    That's beyond my capabilities. The above solution is good enough for me so I've never tried to find something better.