How to get a spawned object to face the side of the camera

8738430--1183371--ezgif-5-1858f8aa5d.gif
I want the spawned gameobject to face the side of the camera point of view.

Vector3 DropPosition = Playercamera.transform.position + Playercamera.transform.forward;
Quaternion Droprotation = Playercamera.transform.localRotation;

DroppedItem = Instantiate(ItemsToDrop.AutomaticSidearm, DropPosition, Droprotation);

DroppedItem.transform.rotation = Quaternion.Euler(0,90,0);

The camera is a child, and the y axis uses a modulo operator, it also uses Quaternion.Euler() to rotate. I’m not sure if this has a effect of the spawned item.

if (CursorLock == true)
        {
         Cursorfloat = 1;
        }
        else
        {
            Cursorfloat = 0;
        }

        float MouseX = Input.GetAxisRaw("Mouse X") * Sens * Time.deltaTime * Cursorfloat;
        float MouseY = Input.GetAxisRaw("Mouse Y") * Sens * Time.deltaTime * Cursorfloat;
 
        XRotation -= MouseY;
        YRotation += MouseX;

        // This line keeps the YRotation within -360 360.
        YRotation = (YRotation + MouseX) % 360;

        XRotation = Mathf.Clamp(XRotation, -90, 90);
        transform.rotation = Quaternion.Euler(XRotation,YRotation,0);

Have an existing camera-child object with your desired transforms, and copy those to the new object, that’s what I do. :wink:

You should use localRotation for all child objects if you haven’t already.

1 Like