- Home /
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);
}
}
}
Forgive me if I am a bit confused, but you are trying to rotate the camera, independent of the ship, in order to simulate looking around. But are concerned that the rotation no longer matches the ship? I thought that was what you were trying to accomplish? If you are trying to reorient it to the ship when they release the stick, just set the rotation to match the ship rotation with the stick is centered.
Sorry if I am misunderstanding something here. -Larry
I'd like the player to be able to turn their head in the cockpit, but currently, if the player is able to turn their head, the camera's rotation no longer matches the ship in its default state. I I'd like the camera to rotate on the parent's axis, so let's say that the ship starts at 0,0,0 rotation (Which it does) and I spin the ship around to face 180,90,0 rotation, the camera (when the player is not using the left stick) matches that 180,90,0 rotation. Then when the player moves the left stick to the left, the camera turns up to 90 degrees in that direction. I What is currently happening is if the player is able to turn the camera, the camera no longer stays with the ship, and the camera's focal point is still 0,0,0 regardless of which way the ship is pointed.
Side note, I currently have the head-turn camera to be a function of the left stick's -1 to 1 axis.
Answer by Larry-Dietz · Dec 28, 2017 at 05:11 AM
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 :)
-Larry
So when the left stick is centered (in its dead zone) the camera is facing forward with the ship. When the left stick is tilted, the camera turns with the stick, but is still relative to the ship.
To expand on my earlier example, let's say I've turned the ship to 180,90,0. I want the camera to also face 180,90,0 when the left stick is in its dead zone. If I push the left stick all the way to the left, I want the camera rotation to now be 180,0,0 and if I push it to the right the rotation would be 180,180,0.
Ok, I think I understand now. Sorry for the confusion.
At first glance, I would suggest taking out the + ship.rotation.x and + ship.rotation.y in your cockpit.transform.rotation code.
Since you have the cockpit as a child of the ship, it should already be matching the rotation of the ship, and as long as the cockpit is rotated to 0,0,0 it would be looking directly forward out the front of the ship, no mater what the ships rotation is. So your looking would be relative to the forward direction of the ship.
-Larry
Will give it a go, but I'm fairly sure I've tried that already. Still, worth a shot. I'll let you know what happens.
Answer by Rehtael · Jan 02, 2018 at 04:41 AM
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?