Unity3d Camera follows the protagonist to move

Source: Internet
Author: User

This binds a script to the main camera.

The script is written as:

Using unityengine;using System.collections;public class Camerafollow:monobehaviour {//main camera Follow lead move public float X    Margin = 1f;    public float ymargin = 1f;    public float xsmooth = 8f;    public float ysmooth = 8f;    Public Vector2 Maxxandy; Public Vector2 minxandy;//with this for initialization private Transform player;void Start () {
Here get is the binding protagonist, need to follow the move together, to get the protagonist's property player = Gameobject.findgameobjectwithtag ("Pk_0"). Transform; maxxandy.x = 10; MAXXANDY.Y = 10;} BOOL Checkxmargin () {return mathf.abs (transform.position.x-player.position.x) > Xmargin; } bool Checkymargin () {return mathf.abs (TRANSFORM.POSITION.Y-PLAYER.POSITION.Y) > Ymargin; }//update is called once per framevoid update () {Trackplayer ();} Trailing protagonist void Trackplayer () {float targetx = transform.position.x; float targety = TRANSFORM.POSITION.Y; if (Checkxmargin ()) {targetx = Mathf.lerp (transform.position.x, player.position.x, Xsmooth * Time.delta Time); } if (Checkymargin ()) {targety = Mathf.lerp (TRANSFORM.POSITION.Y, PLAYER.POSITION.Y, Xsmooth * T Ime.deltatime); } Targetx = Mathf.clamp (Targetx, minxandy.x, MAXXANDY.Y); Targety = Mathf.clamp (targety, MINXANDY.Y, MAXXANDY.Y); Transform.position = new Vector3 (Targetx, targety,transform.position.z); }}

:

Unity3d Camera follows the protagonist to move

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.