private void Start() { Debug.Log("INITIALIZING"); snapper = gameObject.GetComponent <snappable>(); snapper.Snap(); goalPos = transform.position; }
void snap() { //seek the closest snappable Gameobject. foreach (GameObject item in GameObject.FindGameObjectsWithTag("snappable")) { snappingComponent = (item.GetComponent("snappable") as snappable); // if this is the first item in the list, this will trigger and make sure it is not null if (snappingTo == null && snappingComponent.free) { if (notYetReceivedKey || key == snappingComponent.key) { snappingTo = item; } //checks if the keys are the same } //checks if the item of the list is closer to the snappingTo, and sets the closer one. else if (getDistance(item) < getDistance(snappingTo) && snappingComponent.free) { if (notYetReceivedKey || key == snappingComponent.key) { snappingTo = item; } //make sure that the key is the same as the slot, so you cant crossover moves } } //snap to the closest snappable GameObject, and send it a message that it is occupied. if (notYetReceivedKey) { key = (snappingTo.GetComponent("snappable") as snappable).key; // make sure we get the key notYetReceivedKey = false; // make sure we are not swapping keys around } rt.position = snappingTo.transform.position; //move to the object to its snappedPosition gameObject.transform.SetParent(snappingTo.transform); //setting the parent to the new gameObject so it scrolls in a list, or in animations. snappingTo.SendMessage("onSnap", gameObject); //send the slot a message of arrival. }