Allows the camera to smoothly follow the object and the camera to follow the object

Source: Internet
Author: User

Allows the camera to smoothly follow the object and the camera to follow the object
Sometimes when we are playing a third-person game and want the camera to smoothly follow the object, the following code can quickly achieve this effect.

Using UnityEngine; using System. collections; public class CameraMover: MonoBehaviour {public Transform follow; // tansform public float distanceAway = 5.0f; // indicates the distance from public float distanceUp = 2.0f to the rear of the target; // The height of public float smooth = 1.0f above the target; // The interpolation coefficient private Vector3 camPosition; // the position of the target void LateUpdate () {// calculate the position where the camera should be located, camPosition = follow. position + Vector3.up * distanceUp-follow. forward * distanceAway; // interpolation to the camera's expected position transform. position = Vector3.Lerp (transform. position, camPosition, smooth * Time. deltaTime); // a function in tranform, used to make an object forward to the target transform. lookAt (follow); // understood as looking at the target }}

 

Related Article

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.