Rotate camera to a specific object

Source: Internet
Author: User

Sets an object so that camera can automatically fly to the current position from now on.

1. Set a position. You can place a cube that displays its position in front of it. You can also write a script to set the location here.

2. Use Functions

Mobile function transform. Position = vector3.slerp (positionstart, positionend, time * 0.001 );

Rotation function transform. Rotation = Quaternion. slerp (startrotation, aimrotation, time. Time * 0.00001 );

3. Add a function in the switch script that controls automatic roaming in the original FPC camera: press run to automatically record the current position, rotation; press FPS to restore the original position;

4. aimrotation:

4.1 because my camera was rotated 180 degrees at the beginning, you need to add

Aimrotation = inverse (cuberotation );

Aimrotation. Y = 180 degrees + aimrotation. Y;

4.2 when moving to aimpostion, the FPS is slightly moved, so the judgment is added. When FPS is very close to aimpostion, the position is directly assigned to the FPS;

Time

Time. Time the execution time of the frame is earlier than the start time.

Time. deltatime * 10 move 10 meters per frame

This script is a control function I wrote that first redirects the camera to the target object and then flies to the target object in this direction, including fuzzy cameras when the object is very close to the target, then restore to normal

VaR startobject: gameobject; var aimobject: gameobject; var aimobjectlocation: gameobject; var maincamera: Camera; private var positionstart: vector3; private var positionend: vector3; private var aimrotation: callback; private var startrotation: quaternion; private var distance: Float = 10; private var angle: Float = 10; private var isblur: Boolean = false; private var iscloseenough: Boolean = False; private var relativepos: vector3; private var rotationfinished: Boolean = false; private var angledefinition: Boolean = false; private var translatebegin: Boolean = false; function Update () {positionstart = startobject. transform. position; positionend = aimobjectlocation. transform. position; if (! Translatebegin) {relativepos = aimobject. Transform. Position-positionstart; If (transform. Position = positionend) {rotationfinished = true;} else {If (relativepos! = Vector3.zero) {aimrotation = Quaternion. lookrotation (relativepos);} else {rotationfinished = true;} var angle = Quaternion. angle (transform. rotation, aimrotation); If (angle! = 0) {rotationfinished = false; // distance = 10;} else {rotationfinished = true ;}} if (! Rotationfinished) {If (angle> 0.1) {print ("rotationlast"); transform. rotation = Quaternion. slerp (startobject. transform. rotation, aimrotation, 0.15); // normal velocity} else {print ("rotationthesame"); rotationfinished = true; transform. rotation = aimrotation; translatebegin = true ;}} if (translatebegin) {distance = vector3.distance (transform. position, positionend); print (distance); If (distance <0.1) {print ("distance <0.1"); transform. position = positionend; translatebegin = false; waittoblur (); // maincamera. orthographic = true; transform. rotation = aimobjectlocation. transform. rotation;} else {transform. lookat (aimobject. transform); transform. position = vector3.slerp (positionstart, positionend, 0.08) ;}} function waittoblur () {// suspend execution for waittime seconds var blurcamera = maincamera. getcomponent ("blureffect"); blurcamera. enabled = true; yield waitforseconds (0.5); blurcamera. enabled = false ;}

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.