产生两个游戏对象之间的游戏对象统一

时间:2017-07-10 11:38:02

标签: c# unity3d transform

我是Unity和C#的初学者,我正在尝试用2个原子游戏对象创建一个分子游戏对象。这里的主要概念是当两个原子游戏物体彼此靠近时,它们会有所反应并试图形成一种联系。

所以我目前正在尝试做的是在两个原子游戏对象之间产生一个圆柱形游戏对象(作为一个键)。我成功地在原子对象之间产生圆柱体,但是我坚持在这两个原子游戏对象之间正确定位圆柱体。

这就是目前的样子

Molecule Image

这是我创建分子的代码

 private IEnumerator DelayedMoleculeFormation(Atom atomOne, Atom atomTwo)
{
    yield return 0;

    if (atomOne == null || atomTwo == null) yield return 0;

    Transform atomOneTransform = atomOne.transform; // Transform of first atom
    Transform atomTwoTransform = atomTwo.transform; // Transform of second atom

    GameObject atomOneGameObject = atomOne.gameObject; //GameObject of first atom
    GameObject atomTwoGameObject = atomTwo.gameObject;  //GameObject of second atom

    // if both atoms are ready to form bonds
    //if (CanFormBond(atomOne, atomTwo)) this check is not required (nor should be done again) as DelayedMoleculeFormation will only be called if it already passed 
    // CanFormBond will also change reactiveCount so consecutive CanFormBond can return false
    {
        #region AddBond

        Transform atomOneReference = null, atomTwoReference = null;

        GameObject molecule = null;

        if(atomOne.isConsumed)
        {
            if(atomOne.transform.parent) //If an atom is inside a molecule
            molecule = atomOne.transform.parent.gameObject;
        }
        else if (atomTwo.isConsumed)
        {
            if(atomTwo.transform.parent) //if atom two is inside a molecule
            molecule = atomTwo.transform.parent.gameObject;
        }


        if (atomOne.isConsumed)
        {
            atomOneReference = atomOneTransform;
            atomTwoReference = atomTwoTransform;

            atomTwoTransform.LookAt(atomOneTransform.position);
        }
        else
        {
            atomOneReference = atomTwoTransform;
            atomTwoReference = atomOneTransform;

            atomOneTransform.LookAt(atomTwoTransform.position);
        }


        if (molecule == null)
        {
            molecule = Instantiate(Resources.Load("MoleculePrefab")) as GameObject;

            var pos = (atomTwo.transform.position - atomOne.transform.position) / 2.0f + atomTwo.transform.position;
            molecule.transform.position = pos;
            atomTwo.transform.LookAt(atomOne.transform.position);
        }


        //Debug.Log(atomOneReference.name + " " + atomTwoReference.name);

        GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
        cylinder.tag = "Cylinder";
        cylinder.AddComponent<Rigidbody>().useGravity = false;
        cylinder.GetComponent<Collider>().enabled = true;
        cylinder.AddComponent<BreakBond>();

        var dirVector = atomOne.transform.position - atomTwo.transform.position;
        cylinder.transform.position = (atomTwo.transform.position - atomOne.transform.position) / 2.0f + atomOne.transform.position;
        Debug.Log("Atom 1"+atomOne.transform.position+"\n atom2"+atomTwo.transform.position+"\n cylinder"+cylinder.transform.position);
        cylinder.transform.rotation = Quaternion.FromToRotation(Vector3.up, atomTwoTransform.position - atomOneTransform.position);

        atomOneReference.transform.parent = molecule.transform;
        atomTwoReference.transform.parent = molecule.transform;

        cylinder.transform.parent = molecule.transform;

        StartCoroutine(SetAtomPosition(atomOneReference, atomTwoReference, cylinder.transform));

        Vector3 localScale = new Vector3(0.01f, bondLength, 0.01f);
        cylinder.transform.localScale = new Vector3(0.01f, 0.001f, 0.01f);

        StartCoroutine(StartAnimation(cylinder, localScale, bondFormationSpeed, atomOneGameObject, atomTwoGameObject));
        #endregion

        #region setAtomsConsumed
        atomOne.isConsumed = true;
        atomTwo.isConsumed = true;

        atomOne.AddConnection();
        atomTwo.AddConnection();
        #endregion
    }
}

这种方法用于调整位置和更多的东西

  private IEnumerator SetAtomPosition(Transform atomOne, Transform atomTwo, Transform cylinder)
{
    while (Vector3.Distance(atomOne.position, atomTwo.position) > atomDistance)
    {
        atomTwo.position = Vector3.MoveTowards(atomTwo.position, atomOne.position, Time.deltaTime * 0.1f);
        yield return 0;
    }

    while (Vector3.Distance(atomOne.position, atomTwo.position) < atomDistance)
    {
        atomTwo.position = Vector3.MoveTowards(atomTwo.position, -atomTwo.forward * 1000f, Time.deltaTime * 0.1f);
        yield return 0;
    }

    cylinder.transform.position = (atomTwo.position - atomOne.position) / 2.0f + atomOne.position;
 //   cylinder.transform.rotation = Quaternion.FromToRotation(Vector3.up, atomTwo.position - atomOne.position);

    #region JoinAtoms

    cylinder.gameObject.AddComponent<FixedJoint>();
    cylinder.gameObject.AddComponent<FixedJoint>();

    var cylinderJoints = cylinder.GetComponents<FixedJoint>();

    cylinderJoints[0].connectedBody = atomOne.GetComponent<Rigidbody>();
    atomOne.GetComponent<Atom>().joint = cylinderJoints[0];
    //cylinderJoints[0].breakForce = breakForce;

    cylinderJoints[1].connectedBody = atomTwo.GetComponent<Rigidbody>();
    atomTwo.GetComponent<Atom>().joint = cylinderJoints[1];
    //cylinderJoints[1].breakForce = breakForce;
    #endregion

    yield return null;
}

1 个答案:

答案 0 :(得分:0)

快而又肮脏,但我希望你能用它做点什么:)

唯一的假设是圆柱体沿z轴方向(如果你不想要它,你需要将四元数与另一个旋转相乘),所以要把它作为一个空的游戏对象的子节点并沿x旋转90°。

public class Bond : MonoBehaviour {

        public GameObject atom1;
        public GameObject atom2;
        public GameObject bond;

        void Start () {

            Vector3 halfDistance = (atom2.transform.position - atom1.transform.position) * 0.5f;

            bond.transform.position = atom1.transform.position + halfDistance;

            Quaternion bondRotation = Quaternion.LookRotation(halfDistance); 

            bond.transform.rotation = bondRotation;

            bond.transform.localScale = new Vector3(0.5f, 0.5f, halfDistance.magnitude);
        }
    }