Search Unity

Snapping an object to another object

Discussion in 'Scripting' started by Raelin_, Jul 25, 2014.

  1. Raelin_

    Raelin_

    Joined:
    Jul 16, 2014
    Posts:
    20
    is there any way i can get an object to snap to another object and freely move around that object? to be more specific i have an object that follows my mouse cursor(it is used to place structures in the game) and i want it to be snapped to a sphere. is there any way to do this?
     
  2. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    If you mean you want it to move along the surface of a sphere?

    You can use the information from a raycast to work out where it should be and its orientation from the raycastHit's normal vector.
     
  3. Raelin_

    Raelin_

    Joined:
    Jul 16, 2014
    Posts:
    20
    Sorry im new to unity, im not really sure what that means
     
  4. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    A raycast is a way to fire a ray into a scene and find out what it hits.

    http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

    And if you gather information on what you hit with the RayCastHit structure you will get the surface normal (the upward pointing vector) for that point on the sphere.

    http://docs.unity3d.com/ScriptReference/RaycastHit.html

    Using the point and normal information (both in the RayCastHit) then you can position your structure on the sphere under the mouse pointer.
     
  5. Raelin_

    Raelin_

    Joined:
    Jul 16, 2014
    Posts:
    20
    AWESOME, that worked awesomely, thank you so much, but 1 more question, is there any way i can set it so it can only be instantiated on 1 sphere instead of all them(i have multiple spheres spinning around another one, basically a solar system) and is there any way i can make the instantiated object follow (move with) the sphere i put it on?
     
  6. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
  7. Raelin_

    Raelin_

    Joined:
    Jul 16, 2014
    Posts:
    20
    I tried to do that but it only set the original building as a child of the planet(the instantiated objects kept spawning without a parent), i looked it up and couldn't get it working, any suggestions? heres the code
    Code (JavaScript):
    1.     var building : GameObject;
    2.     var planet1 : GameObject;
    3.    
    4.    
    5.    
    6.    
    7.     var myObject : GameObject = Instantiate(building,transform.position,transform.rotation);
    8.     myObject.transform.parent = transform;
    9.     function Update ()
    10.     {
    11.    
    12.     if(Input.GetMouseButtonDown(0))
    13.     {
    14.     Build();
    15.     }
    16.     }
    17.     function Build()
    18.     {
    19.     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    20.     var hit : RaycastHit;
    21.     if (Physics.Raycast (ray, hit, 100))
    22.     {
    23.     Debug.DrawLine (ray.origin, hit.point);
    24.     Instantiate(myObject,hit.point,Quaternion.identity);
    25.    
    26.     }
    27.     }
     
  8. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    In the build function, you want a variable to the gameObject created and then you need to set it's parent to the transform of the object hit by the raycast.
     
  9. Raelin_

    Raelin_

    Joined:
    Jul 16, 2014
    Posts:
    20
    how would i set a variable to the created gameobject?
     
  10. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    the same way you do in lines 7 and 8;
     
  11. Raelin_

    Raelin_

    Joined:
    Jul 16, 2014
    Posts:
    20
    that worked but now its spawning the buildings away from the planet(they are still moving with it, just a large distance off of its surface)
     
  12. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Code (CSharp):
    1.  
    2.     function Build()
    3.     {
    4.     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    5.     var hit : RaycastHit;
    6.     if (Physics.Raycast (ray, hit, 100))
    7.     {
    8.        Debug.DrawLine (ray.origin, hit.point);
    9.        var myObject : GameObject=  Instantiate(myObject,hit.point,Quaternion.identity);
    10.        myObject.transform.parent = hit.transform;
    11.    
    Your code should look a bit like this, although you should set the Rotation based on the hit normal.
     
    Raelin_ likes this.
  13. Raelin_

    Raelin_

    Joined:
    Jul 16, 2014
    Posts:
    20
    you are a genius, thank you so much for all the help :D
     
  14. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    I wish! Glad I could help.