TaiZhouCangChu_VRanime/Assets/动画/Scripts/Morder/SetParentScript.cs

51 lines
1.5 KiB
C#

using UnityEngine;
using System.Collections;
public class SetParentScript : MonoBehaviour
{
// Assign these in the Inspector
public GameObject objectA; // The object you want to become a child and to lock/unlock
public GameObject objectB; // The object to become the parent
private Rigidbody rbA; // Rigidbody of objectA
// Start is called before the first frame update
void Start()
{
// Check if objectA has a Rigidbody component
rbA = objectA.GetComponent<Rigidbody>();
if (rbA == null)
{
// Log an error if no Rigidbody is found
Debug.LogError("No Rigidbody found on objectA");
return;
}
// Lock the Rigidbody by setting constraints
LockMotionAndRotation();
// Call the methods after 3 seconds
Invoke("SetParent", 3f);
Invoke("UnlockMotionAndRotation", 3f);
}
// Method to set the new parent of the object
void SetParent()
{
// Setting objectA as a child of objectB
objectA.transform.SetParent(objectB.transform);
}
void LockMotionAndRotation()
{
// Lock position and rotation by setting constraints
rbA.constraints = RigidbodyConstraints.FreezePosition | RigidbodyConstraints.FreezeRotation;
}
void UnlockMotionAndRotation()
{
// Unlock all constraints to allow movement and rotation again
rbA.constraints = RigidbodyConstraints.None;
}
}