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

Snapping Objects At Runtime

Discussion in 'Scripting' started by g0tNoodles, May 30, 2011.

  1. g0tNoodles

    g0tNoodles

    Joined:
    Nov 28, 2010
    Posts:
    193
    Hi people, I am creating a game where I need an object to snap into position when I enter a trigger. I know the trigger works because I used the destroy command on it, and that worked fine.

    When I drag my item in to the trigger and take my finger off the mouse button I would like it to snap to that pos/gameobject, If it is not in a trigger then I would like it to return to another point in the game world (a kind of item bar).

    Anyway, here is the code I have tried and I know it will probably seem really obvious and silly of me to ask this...

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4.  
    5. public class SnapPeg : MonoBehaviour {
    6.  
    7. public Transform holeTransform;
    8.  
    9. public Transform pegTransform;
    10.  
    11. // Use this for initialization
    12. void Start () {
    13.  
    14. }
    15.  
    16. // Update is called once per frame
    17. void Update () {
    18.  
    19. }
    20.  
    21. void OnTriggerEnter(Collider other)
    22. {
    23.     pegTransform.transform.position = holeTransform.transform.position;
    24.     //Destroy(peg);
    25. }
    26.  
    27. }
    Thanks,

    Cas