unity3d – Unity 3D fps view – objects placing at center point

I’ve searched around and can’t seem to find a write up on the calculations I might need for placing an object onto another one without them placing halfway inside of eachother.

private void MovePlaceableObject()
    {
        Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);

        Debug.DrawRay(ray.origin, ray.direction * 20f);


        RaycastHit hit;

        if(Physics.Raycast(ray, out hit, 15f))
        {
            Vector3 newPosition = hit.point;

            currentPlaceableObject.transform.position = newPosition; //move object to where we have the mouse

            currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal); //rotation equal to the current rotation of our hitinfo, then up
        }
    }



private void HandeNewObjectHotkey()
    {
        if (Input.GetKeyDown(newObjectHotkey))
        {
            if(currentPlaceableObject == null)
            {
                currentPlaceableObject = Instantiate(placeableObjectPrefab);
                //currentPlaceableObject.GetComponent<BoxCollider>().enabled = false;
            }
            else
            {
                Destroy(currentPlaceableObject); //pressing the button again will destroy current object
            }
        }
    }

I’ve tried storing the object I’m looking at’s size to floats for x,y,z and a vector3 for it’s position, and tried fiddling around with adding those to the newPosition with the hit.point to no avail.

I’ve wracked my brains for about a day now, i’m terrible at math, however I know that you must need to get the current object your looking ats position or size, then factor that into your placement right?

Read more here: Source link