I’ve used the MouseOrbit Script from the Assets and placed it on my helicopter in order to create a cockpit view that the player can rotate the camera around with the mouse. I altered abit and it works smoothly, however when I try to rotate my helicopter, the camera doesnt align itself to rotate with the helicopter. I’ve placed the script on the camera that is linked to the helicopter object
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
x = ClampAngle(x, xMinLimit, xMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
How do I fix this to allow my camera view to link with the helicopter whenever it rotates whilst it rotates the view around?
Your question is related to a basic parenting issue, which is not so easily resolved in code.
Basically, just drop your camera as a child of the helicopter object in your game scene.
You’ll immediately see that when you rotate the helicopter, the camera will rotate alongside it… fighting with your mouse input.
Usually, what you need to do in such cases is adapt your mouse input script to ‘ease’ the camera rotation when you’re inside the helicopter, applying interpolation or dampening of the input values.
I assume you had the same issues I was having. I wanted to be able to keep my “mouseorbit” 3rd person camera aligned to the local axis of an aircraft, however my X and Y inputs were not being reoriented to the aircraft axes during flight. Instead, pressing up on my joystick’s hat switch would lead to a camera change in the up-direction in the world axis. This occurred whenever my aircraft was not wings level. Same issue for adjusting the yaw orientation about the aircraft. I have an aircraft as the parent and my camera “childed” to it. The original MouseOrbit script included in unity uses Euler angles, but I made a few changes for it to use Quaternions for the transform rotations. Hope this helps!
var target : Transform;
var distance = 50.0;
var height = 5;
var lookSpeed = 250.0;
var moveSpeed = 50.0;
private var rotationX = 0.0;
private var rotationY = 0.0;
function Start () {
var angles = transform.localEulerAngles; // target WORLD SPACE
rotationX = angles.x; // Eulerangles.x = rotation z degrees about z axis target WORLD SPACE
rotationY = angles.y; // Eulerangles.y = rotation x degrees about x axis target WORLD SPACE
rotationZ = angles.z; // Eulerangles.z = rotation y degrees about y axis target WORLD SPACE
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (target)
{
if (Mathf.Abs(Input.GetAxis("LookPitchAxis")) > 0.05)
{rotationY -= Input.GetAxis("LookPitchAxis") * lookSpeed * 0.02;}
if (Mathf.Abs(Input.GetAxis("LookYawAxis")) > 0.05)
{rotationX += Input.GetAxis("LookYawAxis") * lookSpeed * 0.02;}
// Reset view button action (directly behind aircraft)
if (Input.GetButtonDown ("ResetView")) {
rotationX = target.transform.eulerAngles.x; // rotation.x // target WORLD SPACE target.transform.ect
rotationY = target.transform.eulerAngles.y; // rotation.y // target WORLD SPACE
rotationZ = target.transform.eulerAngles.z; // rotation.z // target WORLD SPACE
}
transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
transform.position = transform.rotation * Vector3(0.0, 0.0, -distance) + target.position;
}