I have managed to snap my game object to the bottom of another game object, but once I have instantiated a second game object, it doesn't snap to the instantiated game object. I want to be able to have the instantiated game object to snap at the bottom of another instantiated game object once it has been loaded, similar to a daisy chain. Currently, it will only snap to the original "bottom" game object but not the second. Any suggestion?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SnapToMe : MonoBehaviour
{
public GameObject target = null;
public float snapDistance = 2f;
void FixedUpdate()
{
if (target == null) return;
if (Vector3.Distance(transform.position, target.transform.position) <= snapDistance)
{
target.transform.parent = transform;
target.transform.localRotation = Quaternion.identity;
target.transform.localPosition = Vector3.zero;
target.GetComponent<DragObject>().enabled = false;
}
}
}
public void LoadDrillString()
{
if (!isCreatedDrillbit)
{
GameObject instance = Instantiate(Resources.Load("Drillstring-2", typeof(GameObject))) as GameObject;
GameObject TipSnapPoint = GameObject.Find("Bottom");
SnapToMe tipsnapper = TipSnapPoint.GetComponent<SnapToMe>();
tipsnapper.target = instance;
tipsnapper.snapDistance = 2f;
}
}