- Home /
How to rotate camera around 3D object without breaking camera rotation
I have a problem making rotate around camera work properly. I made a script where I can rotate it around how I want except for the fact that camera itself gets rotated how I don't want it. Here's video of what happens: https://youtu.be/qJ04WCyueYY Here's my camera script:
#pragma strict
var Ship : Transform;
var LookAt : GameObject;
var Distance : float;
var MaxZoomOut : float;
var MaxZoomIn : float;
var RotationSpeed : float = 5.0;
var UpsideDown : boolean;
function Start () {
}
function Update () {
//Check Distance From Ship
Distance = Vector3.Distance(transform.position, LookAt.transform.position);
//Zoom In
if (Input.GetAxis("Mouse ScrollWheel") > 0 && Distance >= MaxZoomIn) {
transform.position += transform.forward*5;
}
//Zoom Out
if (Input.GetAxis("Mouse ScrollWheel") < 0 && Distance <= MaxZoomOut ) { // back
transform.position -= transform.forward*5;
}
//Fix Zoom Out
if (Distance >= (MaxZoomOut+1)) {
transform.position += transform.forward;
}
//Fix Zoom In
if (Distance <= (MaxZoomIn-1)) {
transform.position -= transform.forward;
}
//Rotations-=-=-=-=-
if (Input.GetMouseButton(2)) {
transform.RotateAround (LookAt.transform.position, transform.TransformDirection( Vector3.up ), (Input.GetAxis("Mouse X")*5));
transform.RotateAround (LookAt.transform.position, transform.TransformDirection( Vector3.left ), (Input.GetAxis("Mouse Y")*5));
}
}
I want the camera to be able to look fully around the ship like in the video but remove the part where the camera rotates out of ships local axis. Adding transform.LookAt made the camera lock it's rotation and not able to rotate the view when I am turning the ship itself.
Comment