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