Can't rotate a camera with another object

I’m making a piloting game akin to the classic X-wing and TIE fighter games, and am using a 360 controller to control the ship. Right stick controls the pitch and roll of the ship, and this works perfectly. I want the left stick to allow players to look left, right, and up in first person. I can get to the camera working to that extent, but when the camera is able to look in those directions, it no longer matches the rotation (x, y, z) of the ship.

Further explained: when the camera can’t rotate via the left stick, it works fine, but when it can rotate via the left stick, it uses global rotation (I think that is what is happening)

I have the ship as a parent object (“ship”), and I have an empty game object (“holder”), with the camera (“cockpit”) as a child of that empty game object.

I’ve been trying everything I can think of, can anyone help me figure this out?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerShipScript : MonoBehaviour

    public float forwardSpeed;
    public float reverseSpeed;

    public float pitchSpeed;
    public float rollSpeed;
    public float yawSpeed;
    public float pitchTorque;
    public float rollTorque;
    public float yawTorque;

    public float pitchAxisVal;
    public float rollAxisVal;
    public float yawAxisVal;

    public float pitchCamVal;
    public float yawCamVal;
    public float rollCamVal;

    public float thrustSpeed;
    public float thrustRate;
    public float thrustMax;
    public float thrustMin;

    public Rigidbody ship;

    public Transform holder;

    public Camera cockpit;

    private float cockpitCameraAxis;

    private void Start()
        thrustSpeed = 0.0f;
        thrustMax = 30.0f;
        thrustMin = 0.0f;


    private void Update()

        pitchCamVal = Input.GetAxis("LeftV");
        yawCamVal = Input.GetAxis("LeftH");

        pitchAxisVal = Input.GetAxis("RightV");
        rollAxisVal = Input.GetAxis("RightH");
        yawAxisVal = ship.rotation.y;

        //transform.Rotate(Vector3.left, (pitchAxisVal * pitchSpeed) * Time.deltaTime);
        //transform.Rotate(Vector3.forward, (rollAxisVal * -rollSpeed) * Time.deltaTime);
        ship.AddTorque(transform.right * -pitchAxisVal * pitchTorque);
        ship.AddTorque(transform.forward * -rollAxisVal * rollTorque);

        cockpit.transform.rotation = Quaternion.Euler((pitchCamVal * 90f) + ship.rotation.x, (yawCamVal * 90f) + ship.rotation.y, ship.rotation.z);

        if (Input.GetKeyDown(KeyCode.Joystick1Button0))
            Debug.Log(holder.transform.rotation.x + " , " + holder.transform.rotation.y + " , " + holder.transform.rotation.z);
            Debug.Log(ship.transform.rotation.x + " , " + ship.transform.rotation.y + " , " + ship.transform.rotation.z);

    private void FixedUpdate()

        ship.transform.Translate(Vector3.forward * thrustSpeed / 25.0f);

        if (Input.GetKey(KeyCode.Joystick1Button7) && (thrustSpeed < thrustMax))
            thrustSpeed = thrustSpeed + thrustRate;
            Debug.Log("+: " + thrustSpeed);

        if (Input.GetKey(KeyCode.Joystick1Button6) && (thrustSpeed > thrustMin))
            thrustSpeed = thrustSpeed - thrustRate;
            Debug.Log("-: " + thrustSpeed);

Sounds to me like simple resetting the camera’s rotation to match the ship when the left stick is centered would solve the problem.

Or are you saying that when the code to handle the rotation is present, the camera stops matching the ship even when they are not using the stick?

Sorry for my confusion. Just trying to wrap my head around the problem before trying to figure out a solution for the wrong problem :slight_smile:


Been poking and prodding at this problem for a long while now, and I still can’t figure out what is going wrong. Is there anyone out there with 2 cents?