Summary of a camera control class

Source: Internet
Author: User
Summary of a camera control class
Functions:

Through Mouse control, the camera rotates around the model object to observe the model object.

 

Design Concept:

First, the camera's current orientation is used to calculate the target orientation;

Then, the camera is moved to the target position frame by frame through interpolation.

 

Process description:

1. Calculate the camera's target orientation.

 

First, the angle between the axial angle of the camera's local coordinate system and the axial angle of the world coordinate system is calculated as the initial value of the azimuth. Note: Here we only need to rotate the changed axis of the camera. Here we use the X and Y axes.

 

Modify the angle value in real time during mouse control.

 

Limit the modified angle value to meet our needs.

 

Convert the angle value after the limit processing from the representation of the Euclidean angle to the representation of the Quaternary element, and save it as the camera's target orientation.

2. By interpolation between the current orientation and the target orientation of the camera, the camera is moved to the target orientation at a certain speed.

 

Source code: bound to a camera object

/// <Summary>

/// Transform component of the rotating axis (model object)

/// </Summary>

Public transform _ tfmrotaxis;

/// <Summary>

/// Mobile buffering of the camera

/// </Summary>

Public float _ fzoomdmp = 5f;

/// <Summary>

/// Camera movement speed

/// </Summary>

Public float _ fzoomrate = 240f;

/// <Summary>

/// Sensitivity of moving the mouse x

/// </Summary>

Public float _ fxspe = 6f;

/// <Summary>

/// Sensitivity of moving the mouse in Y direction

/// </Summary>

Public float _ fyspe = 6f;

/// <Summary>

/// Minimum angle limit of the mouse in the X direction

/// </Summary>

Public float _ fminlimitx =-360f;

/// <Summary>

/// Maximum angle limit of the mouse in the X direction

/// </Summary>

Public float _ fmaxlimitx = 360f;

/// <Summary>

/// Minimum angle limit of the Y direction of the mouse

/// </Summary>

Public float _ fminlimity =-360f;

/// <Summary>

/// Maximum angle limit of the Y direction of the mouse

/// </Summary>

Public float _ fmaxlimity = 360f;

/// <Summary>

/// The nearest distance from the camera to the axis of rotation

/// </Summary>

Public float _ fmindist = 0f;

/// <Summary>

/// The maximum distance from the camera to the axis of rotation

/// </Summary>

Public float _ fmaxdist = 20f;

/// <Summary>

/// Offset of the Movement

/// </Summary>

Public vector3 _ vec3taroffset;

// ------------------------------ Private ----------------------------------

/// <Summary>

/// Camera conversion component

/// </Summary>

Private transform _ tfmthis;

/// <Summary>

/// Current distance from the camera to the rotating axis

/// </Summary>

Private float _ fcurtdist;

/// <Summary>

/// Distance from the camera to the target of the Rotating Axis

/// </Summary>

Private float _ ftardist;

/// <Summary>

/// Camera target orientation (orientation)

/// </Summary>

Private quaternion _ quatarrtn;

/// <Summary>

/// Angle between the X axis of the camera and the X axis of the world coordinate

/// </Summary>

Private float _ fxdgre;

/// <Summary>

/// Angle between the Y axis of the camera and the Y axis of the world coordinate

/// </Summary>

Private float _ fydgre;

 

Void awake ()

{

// Cache

_ Tfmthis = transform;

 

_ Fcurtdist = vector3.distance (_ tfmthis. Position, _ tfmrotaxis. position );

_ Ftardist = _ fcurtdist;

 

// Calculate the coordinate axes of the camera's local coordinate system under the world coordinate system

// Angle between the coordinate axes of the global coordinate system

_ Fxdgre = vector3.angle (vector3.right, _ tfmthis. Right); // The angle between the X axis

_ Fydgre = vector3.angle (vector3.up, _ tfmthis. Up); // y axis angle

}

 

Void lateupdate ()

{

// Detect right-click and press

If (input. getmousebutton (1 )){

 

// Calculate the current Axial Angle

_ Fxdgre + = input. getaxis ("Mouse X") * _ fxspe;

_ Fydgre-= input. getaxis ("Mouse y") * _ fyspe;

// Limit the angle of the clip

_ Fxdgre = mathf. Clamp (_ fxdgre, _ fminlimitx, _ fmaxlimitx );

_ Fydgre = mathf. Clamp (_ fydgre, _ fminlimity, _ fmaxlimity );

// Calculates the target orientation (orientation)

_ Quatarrtn = Quaternion. Euler (_ fydgre, _ fxdgre, 0 );

// Interpolation

_ Tfmthis. Rotation = Quaternion. lerp (_ tfmthis. rotation, _ quatarrtn, _ fzoomdmp * time. deltatime );

}

 

Scrollwheelctrl ();

}


/// <Summary>

/// The scroll wheel controls the zoom of the field of view

/// </Summary>

Private void scrollwheelctrl ()

{

_ Ftardist-= input. getaxis ("Mouse scrollwheel") * _ fzoomrate * time. deltatime;

 

_ Ftardist = mathf. Clamp (_ ftardist, _ fmindist, _ fmaxdist );

 

_ Fcurtdist = mathf. lerp (_ fcurtdist, _ ftardist, _ fzoomdmp * time. deltatime );

 

_ Tfmthis. Position = _ tfmrotaxis. Position-(_ tfmthis. Rotation * vector3.forward * _ fcurtdist + _ vec3taroffset );

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.