Unity Code

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

using System.

Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class Merge : MonoBehaviour


{

private float lastClickTime = 0f;


public float doubleClickTimeThreshold = 0.3f;

//唷绾识夜星枰琳∫妹橇嗷绻 Parent 啻章恰压弥嗷盆�


public bool isParent = false;

//幻小胰笛轻幻幻朽婪 gameobj 淝樗业亚 gameobj


public GameObject findObject;

private void Start()


{
isParent = false;
findObject = GameObject.Find("snapPoint1");
}
private void Update()
{

if (isParent==false)
{
findObject.gameObject.GetComponent<Merge>();
/* findObject.gameObject.GetComponent<Collider>().enabled = true;
gameObject.GetComponent<Rigidbody>().isKinematic = false;
findObject.gameObject.GetComponent<Merge>().isParent = false;*/
}
}

public void OnCollisionEnter(Collision col)


{
//Parenting();
if (col.gameObject.CompareTag("Merge"))
{
MeshRenderer colMeshRenderer =
col.gameObject.GetComponent<MeshRenderer>();
MeshRenderer thisMeshRenderer = GetComponent<MeshRenderer>();
if (isParent == false)
{
if (colMeshRenderer != null && thisMeshRenderer != null)
{
// Set the current object as the parent of the collided object
thisMeshRenderer.transform.parent = colMeshRenderer.transform;

isParent = true;
colMeshRenderer.GetComponentInParent<Rigidbody>().isKinematic =
true;

//thisMeshRenderer.GetComponentInParent<Rigidbody>().isKinematic = true;

// Disable the collider of the collided object so that it


doesn't trigger multiple collisions
thisMeshRenderer.GetComponent<Collider>().enabled = false;
Debug.Log("parent = true");
}
}

return;
}

public void OnMouseUp()


{
if (Time.time - lastClickTime < doubleClickTimeThreshold)
{
gameObject.transform.DetachChildren();
isParent = false;
gameObject.transform.position = this.transform.localPosition - new
Vector3 (1,0,0);
findObject.gameObject.GetComponent<Collider>().enabled = true;
gameObject.GetComponent<Rigidbody>().isKinematic = false;
findObject.gameObject.GetComponent<Merge>().isParent = false;
Debug.Log("parent = false");
return;

}
lastClickTime = Time.time;
}

You might also like