Unity3d simple camera follow-up and field rotation zoom

Source: Internet
Author: User

1, realize the camera to follow the protagonist movement

A simple way to do this is to drag the camera directly underneath the player as a child of the player, and the other way is to get the camera and player offset vectors, and set the camera location accordingly, you can achieve a simple cameras follow.

Here we choose the second method, first to add a script to the camera, named Followplayer, the script is very simple do not explain

1  Public classFollowplayer:monobehaviour {2 3         PrivateTransform player;4         PrivateVector3 offsetposition;5 6     //Use this for initialization7     voidStart () {8Player =Gameobject.findgameobjectwithtag ("Player"). Transform;9Offsetposition = transform.position-player.position;Ten transform. LookAt (player.position); One     } A      -     //Update is called once per frame -     voidUpdate () { theTransform.position = Offsetposition +player.position; -  -}

2, field of view Zoom

  The mouse wheel moves closer to the field of view and backward to the field. The principle is to set a variable distance as the modulus of the offset vector, the mouse wheel slide changes the value of the distance, and then set the camera position according to the distance

Where scrollspeed is defined as a float variable used to adjust the speed of the zoom

1 Private floatdistance;2 3 Private voidScrollView ()4     {5Distance =Offsetposition.magnitude;6         //slide forward, slide backward, pull away .7Distance-= Input.getaxis ("Mouse Scrollwheel") *Scrollspeed;8Distance =Mathf.clamp (distance, mindistance, maxdistance);9Offsetposition = offsetposition.normalized *distance;Ten     } One  A //then call the ScrollView method inside the update .

3. Field of view rotation

Use transform. Rotatearound method to rotate the camera according to the position of the player

  

1 Private voidRotateview ()2     {3         //right mouse button press to rotate field of view4         if(Input.getmousebuttondown (1))5Isrotating =true;6         if(Input.getmousebuttonup (1))7Isrotating =false;8 9         if(isrotating)Ten         { OneVector3 originalposition =transform.position; Aquaternion originalrotation =transform.rotation; -Transform. Rotatearound (Player.position, Player.up, Rotatespeed * Input.getaxis ("Mouse X")); -Transform. Rotatearound (Player.position, Transform.right,-rotatespeed * Input.getaxis ("Mouse Y")); the             floatx =transform.eulerangles.x; -             //the range of rotation is 10 degrees to 80 degrees. -             if(X <Ten|| X > the) -             { +Transform.position =originalposition; -Transform.rotation =originalrotation; +             } A              at         } -  -Offsetposition = transform.position-player.position; -}

Unity3d simple camera follow-up and field rotation zoom

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.