What I'm trying to achieve is snapping a child object to the center of its parent object after dragging and dropping it.
The moving object successfully follows the mouse, and drops. It also successfully selects a dynamic parent when it moves over a new node.
What it does NOT do is then snap to the center of its parent. It instead jumps all over the screen and snaps to nothing.
Here is all of my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Object_Moveable: MonoBehaviour {
private Vector2 mousePos;
public float moveSpeed;
public float offset = 0f;
private bool following;
private GameObject SelectedNode;
// Use this for initialization
void Start() {
following = false;
offset += 10;
}
void Update() {
if (Input.GetMouseButtonDown(0) && ((Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position).magnitude <= offset)) {
following = true;
}
if (following) {
transform.position = Vector2.Lerp(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition), moveSpeed);
}
if (Input.GetMouseButtonUp(0)) {
following = false;
//Attach (Node, 0, 0);
transform.localPosition = SelectedNode.transform.position;
}
}
void OnTriggerEnter2D(Collider2D trigger) {
if (trigger.tag == "pipenode") {
transform.SetParent(trigger.transform);
SelectedNode = trigger.gameObject;
}
}
}
(0,0,0)
... \$\endgroup\$