- Home /
Basic Camera rotation and Zoom
Hello,
I am quite new at Unity & Programming so any help would be much appreciated. I am trying to make a camera which rotates and zooms on the centre of the screen rather than on a Transform Target when left shift is held down. Similar to an RTS Camera. I have used some script from Mouse Orbit. My code is below, only problem is the camera now falls down or moves through Terrain when left shift is held down. Any ideas how I cam fix this ?
Thanks for you help
var target : Vector3; var distance :float = 150.0; // distance from camera to Target var targetPoint ;
var xSpeed = 250.0; // max rotate camera speed x axis var ySpeed = 120.0; // max rotate camera spped y axis
var yMinLimit = 5; // min angle y plane var yMaxLimit = 90;// max angle y plane
var distanceMin = 20; // min zoom var distanceMax = 200; //max zoom var zoomSpeed = 70; // Speed to zoom in and out
private var x = 0.0; private var y = 0.0;
function Start () // run this function once 1st time this script is called { 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(Input.GetKey(KeyCode.LeftShift))
{
var ray : Ray = camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));
var hitdist :float = distance;
target = ray.GetPoint(hitdist);
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*zoomSpeed, distanceMin, distanceMax);
var position = rotation * Vector3(0, 0,-distance)+ target;
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); }
please point your program$$anonymous$$g launge in tag, i know only C#!
Your answer
![](https://koobas.hobune.stream/wayback/20220613130801im_/https://answers.unity.com/themes/thub/images/avi.jpg)