Unity first person makes the camera follow the main character move

Source: Internet
Author: User

The using unityengine;using system.collections;//should add character controller components to the machine. public class Cubet:monobehaviour {//here defines a Class, the object that is dragged to is one of the objects of the class public enum rotationaxes {mousexandy = 0, MouseX = 1, Mousey = 2}public rotationaxes axes = Rotationa Xes. mousexandy;public float Sensitivityx = 15f;public float sensitivityy = 15f;public float minimumx = -360f;public float maxi Mumx = 360f;public Float minimumy = -60f;public float Maximumy = 60f;float RotationY = 0f;public Transform cam_transform;p   Ublic Transform per_transform;//Use this for initializationvoid Start () {per_transform = This.transform; Gets the transform of the object, including the rotation angle, position, and so on.   This is equivalent to the pointer pointing to Gameobject cobj = Gameobject.find ("Maincamera"); Gets the camera Cam_transform = Transformcamstart () of the cobj.transform;//camera, if (getcomponent<rigidbody> ())//rotation freezes. The function is used to make the rotation angle not be changed by the physical simulation. Getcomponent<rigidbody> (). Freezerotation = true;} void Camstart () {Vector3 Perpos = per_transform.position;perpos.y + 0.5f;perpos.x + 1f;perpos.z + = 1f;cam_ Transform.position = perpos;//Set the camera position, angle cam_transform.eulerangles = per_transform.eulerangles;} Update is called once per framevoid update () {//Official code if (axes = = Rotationaxes.mousexandy) {Float RotationX = cam_transf Orm.localeulerangles.y + input.getaxis ("Mouse X") * sensitivityx;rotationy + = Input.getaxis ("Mouse y") * SENSITIVITYY; RotationY = Mathf.clamp (RotationY, Minimumy, maximumy); cam_transform.localeulerangles = new Vector3 (-rotationy, RotationX, 0);} else if (axes = = Rotationaxes.mousex) {cam_transform. Rotate (0, Input.getaxis ("Mouse X") * Sensitivityx, 0);} Else{rotationy + = Input.getaxis ("Mouse Y") * Sensitivityy;rotationy = Mathf.clamp (RotationY, Minimumy, maximumy); cam_ Transform.localeulerangles = new Vector3 (-rotationy, transform.localeulerangles.y, 0);} Per_transform.localeulerangles = cam_transform.transform.localeulerangles;//The orientation of the set object is always consistent with the orientation of the camera. }}

Unity first person makes the camera follow the main character 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.