- Home /
Aligning Camera to Object
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
This is the modified MouseOrbit Script:
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var xMinLimit = -90;
var xMaxLimit = 90;
private var x = 0.0;
private var y = 0.0;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
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?
Answer by roamcel · Oct 25, 2011 at 07:04 AM
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.
Actually, the rotation of the helicopter isn't via the mouse input, but by 'A' and 'D' buttons.
Answer by kampma · Oct 29, 2011 at 11:22 PM
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;
@script AddComponentMenu("Camera-Control/Camera Orbit")
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;
}
}
Your answer
Follow this Question
Related Questions
changing the target of camera through script 1 Answer
Camera Movement and angles 2 Answers
Is it possible to shake the camera? 4 Answers
Make Arms Move Slow 0 Answers
Toggle between scripts with a script? 2 Answers