x


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);
}
more ▼

asked May 02, 2011 at 03:44 AM

Jaimie gravatar image

Jaimie
4 6 4 4

please point your programming launge in tag, i know only C#!

May 02, 2011 at 04:17 AM Kool
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4740
x3598
x262

asked: May 02, 2011 at 03:44 AM

Seen: 1524 times

Last Updated: May 02, 2011 at 04:06 AM