如何将多个对象集成到一个基于物理的刚体中?

时间:2018-05-02 00:56:22

标签: c# unity3d

所以我有these个立方体,我可以在场景中拖动。我想要做的就是让这些立方体集成/捕捉/附加或任何你想要的东西。当它们像视频中那样彼此靠近时,我希望它们相互连接并作为一个对象而不是相互拖拽?这是我拖动立方体的代码:

using System.Collections; 

using System.Collections.Generic;

using UnityEngine;

public class dragtest7 : MonoBehaviour
{

public float minY = 0.427f;
public float maxY = 0.427f;

public float minZ = 6.536f;
public float maxZ = 15.528f;

public float minX = -5.5f;
public float maxX = 3.493f;

public float minYR = 0;
public float maxYR = 0;

public float minZR = 0;
public float maxZR = 0;

public float minXR = 0;
public float maxXR = 0;



void OnMouseDrag()
{
    float distance = Camera.main.WorldToScreenPoint(gameObject.GetComponent<Rigidbody>().position).z;
   GetComponent<Rigidbody>().MovePosition(Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance)));
}

void Update()
{
    Vector3 currentPosition = GetComponent<Rigidbody>().position;
    Vector3 currentRotation = GetComponent<Rigidbody>().rotation.eulerAngles;

    currentPosition.y = Mathf.Clamp(currentPosition.y, minY, maxY);
    GetComponent<Rigidbody>().position = currentPosition;

    currentPosition.z = Mathf.Clamp(currentPosition.z, minZ, maxZ);
    GetComponent<Rigidbody>().position = currentPosition;

    currentPosition.x = Mathf.Clamp(currentPosition.x, minX, maxX);
    GetComponent<Rigidbody>().position = currentPosition;

    currentRotation.y = Mathf.Clamp(currentRotation.y, minYR, maxYR);
    GetComponent<Rigidbody>().rotation = Quaternion.Euler(currentRotation);

    currentRotation.z = Mathf.Clamp(currentRotation.z, minZR, maxZR);
    GetComponent<Rigidbody>().rotation = Quaternion.Euler(currentRotation);

    currentRotation.x = Mathf.Clamp(currentRotation.x, minXR, maxXR);
    GetComponent<Rigidbody>().rotation = Quaternion.Euler(currentRotation);
}
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

您需要使用Fixed joint。固定接头允许您将两个刚体连接成一个刚体。基本上在游戏对象和OnCollisionEnter上添加Fixedjoint,您只需将connectedBody作为碰撞对象附加。这应该允许您将两个对象连接为一个。

我没有使用fixedjoint随时工作,所以做了一点挖掘并制作了一个小程序,让两个立方体合并并充当单个对象。它确实作为完整的对象工作。在您的情况下,您将需要多个固定连接来组合多个对象。这是我用来附加两个立方体的小脚本。希望这可以帮助你完成工作。

Rigidbody currentRigidBody;
FixedJoint fixedJointObj;
// Use this for initialization
void Start () 
{
    currentRigidBody = GetComponent<Rigidbody>();
}

// Update is called once per frame
void Update () 
{
    if (Input.GetKey(KeyCode.LeftArrow))
    {
        currentRigidBody.AddForce(new Vector3(-1, 0, 0));
    }

    if (Input.GetKey(KeyCode.RightArrow))
    {
        currentRigidBody.AddForce(new Vector3(1, 0, 0));
    }

    if (Input.GetKey(KeyCode.UpArrow))
    {
        currentRigidBody.AddForce(new Vector3(0, 1, 0));
    }

    if (Input.GetKey(KeyCode.DownArrow))
    {
        currentRigidBody.AddForce(new Vector3(0, -1, 0));
    }
}

private void OnCollisionEnter(Collision collision)
{
    if(fixedJointObj == null )
        fixedJointObj = gameObject.AddComponent<FixedJoint>();

    Rigidbody body2;

    if ((body2 = collision.gameObject.GetComponent<Rigidbody>()) == null)
        return;

    fixedJointObj.connectedBody = body2;
}