Exemplo n.º 1
0
    void InstantiatePart()
    {
        if (!partToPlace)          //If we don't have a part to place
        {
            if (partToPlacePrefab) //but we have a prefab for it
            {
                //instantiate the part on a position way out of sight from the camera
                partToPlace = Instantiate(partToPlacePrefab, -Vector3.up * 2000, Quaternion.identity, myCar.transform) as GameObject;
            }
            //if we don't have a prefab, then the player is either sleeping or haven't decided what to place
        }
        else
        {
            //If we have a part to place

            //Make a raycast from the camera to mouse position
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, Mathf.Infinity))
            {
                //Check what we hit
                CheckHit(hit);
            }

            //Update the part's position
            partToPlace.transform.position = placePos;

            //And if we click
            if (Input.GetMouseButtonUp(0) && socketToPlace != null)
            {
                //...store the Siege part base of the "part to place"
                SiegePart_base placeBase = partToPlace.GetComponent <SiegePart_base>();

                //add it to the list
                PlayerParts.Add(partToPlace.transform);
                //enable it's collider
                partToPlace.GetComponentInChildren <Collider>().enabled = true;

                //assign a target to the joint
                placeBase.AssignTargetToJoint(socketToPlace.parent);

                //disable the socket
                placeBase.DisableSocket(socketToPlace);

                //update the position once more
                partToPlace.transform.position = placePos;

                partToPlace = null;
            }
        }
    }
Exemplo n.º 2
0
    void CheckHit(RaycastHit hit)
    {
        //If we hit a gameobject that has the SiegePart_base script then we hit a part of our siege engine
        if (hit.transform.GetComponent <SiegePart_base>())
        {
            //Store it
            SiegePart_base partBase = hit.transform.GetComponent <SiegePart_base>();

            //Find the closest socket to our hit.point
            socketToPlace = partBase.ReturnClosestDirection(hit.point);

            //if we have a socket, then update the place pos
            if (socketToPlace) //The socket might return null if the part doesn't have any sockets, this avoids errors from that
            {
                placePos = socketToPlace.position;

                //We want to look at the center of the other parts mesh
                //Since the transform.position gives the pivot of a gameobject,
                //it might not always be the correct position we want our part to look
                //thus we simply find the center of that other parts mesh

                Vector3 dir = partBase.rendererToFindEdges.bounds.center - socketToPlace.position;

                dir.Normalize();

                if (dir == Vector3.zero)
                {
                    dir = -socketToPlace.forward;
                }

                Quaternion rot = Quaternion.LookRotation(dir);
                partToPlace.transform.rotation = rot;
                //partToPlace.transform.LookAt(partBase.rendererToFindEdges.bounds.center);
            }
        }
        else
        {
            //If we din't hit an object, hide the part then
            placePos = new Vector3(0, -2000, 0);
        }
    }