original, reproduced please indicate the source @jerochan
Learn the dark light of the Siki, in the learning of the teacher's implementation of the code in addition to find other methods of implementation, in order to facilitate their own independent packaging of the project of rotation, stretching method, so that the coupling degree as low as possible.
Defined
Private Gameobject Playerobject;
Private Camera Cameraobject;
Private Vector3 OffsetPos; Offset (the difference between the camera and the target)
public float Fieldviewmax = 90f; Stretching distance furthest distance
public float fieldviewmin = 10f; Stretch distance Nearest distance
public float scrollspeed = 40f; Tensile speed
public float rotatespeed = 10f; Rotational speed
private bool isrotate = false; Rotation status identifier
Follow and main code
Use Lateupdate to control the camera, lateupdate handle camera logic after update finishes processing the role logic, and prevent the camera from losing the role.
void Start () { Cameraobject = this. Getcomponent<camera>(); Playerobject = Gameobject.findgameobjectwithtag ("Player"); OffsetPos = this.transform.position- playerObject.transform.position; Transform. LookAt (Playerobject.transform); } void lateupdate () { //camera follow (using fixed offset method) this.transform.position = OffsetPos + PlayerObject.transform.position; field of view stretching (using Camera.fieldofview method) Scrollofview (Cameraobject,fieldviewmin, Fieldviewmax, scrollspeed);//Field rotation ( Using the Transform.rotatearound method) Rotateofview (Cameraobject.transform, Playerobject.transform,ref offSetPos,ref Isrotate,rotatespeed); }
Camera Field Stretch code
Roller control camera field stretching (mounted on camera) _camera----> Camera _fieldviewmin ----> Recent field _fieldviewmax ----> Maximum field of view _scrollspeed ----> Tensile speed (default 40)
void Scrollofview (Camera _camera, float _fieldviewmin, float _fieldviewmax,float _scrollspeed=40) { _ Camera.fieldofview-= Input.getaxis ("Mouse scrollwheel") * _scrollspeed; _camera.fieldofview = Mathf.clamp (_camera.fieldofview, _fieldviewmin, _fieldviewmax); }
Camera Vision Rotation Code
Right-drag rotation field of view (mounted on camera) _cameratransform----> Camera transform_targettransform > Target transform_offsetpos----> Offset position of camera and target (transitive) _isrotate----> Rotation status identifier (_rotatespeed)----> rotational speed (default 10)
void Rotateofview (Transform _cameratransform, Transform _targettransform,ref Vector3 _offsetpos,ref bool _isrotate, Float _rotatespeed=10{//When the right mouse button is pressed to toggle the rotation state, lift indicates the end rotation state if (Input.getmousebuttondown (1) ) _isrotate = True; if (Input.getmousebuttonup (1)) _isrotate = False; Rotation state if (_isrotate) {Vector3 originalposition = _cameratransform.position; quaternion originalrotation = _cameratransform.rotation; _cameratransform.rotatearound (_ Targettransform.position, _targettransform.up, _rotatespeed * Input.getaxis ("Mouse X")); _cameratransform.rotatearound (_targettransform.position, _cameratransform.right,-_rotateSpeed * Input.GetAxis (" Mouse Y ")); Limit upper and lower rotation angle if (_cameratransform.eulerangles.x > | | _cameratransform.eulerangles.x <) {_ Cameratransform.position = originalposition; _cameratransform.rotation = originalrotation;}//Update offset _ OffsetPos = _cameratransform.position- _targettransform.position;}}
The relatively intuitive function of writing, do not take notes to show that the great God is welcome to have a better way please advise.
Finally attach PlayerFollow.cs source code
1 usingSystem.Collections;2 usingSystem.Collections.Generic;3 usingUnityengine;4 5 Public classFollowplayer:monobehaviour {6 7 8 PrivateGameobject Playerobject;9 PrivateCamera Cameraobject;Ten PrivateVector3 OffsetPos;//offset (The difference between the camera and the target) One Public floatFieldviewmax = 90f;//stretching distance furthest distance A Public floatFieldviewmin = 10f;//stretch distance Nearest distance - Public floatScrollspeed = 40f;//Tensile Speed - Public floatRotatespeed = 10f;//rotational speed the Private BOOLIsrotate =false;//Rotation Status Identifier - - - voidStart () { + -Cameraobject = This. Getcomponent<camera>(); +Playerobject = Gameobject.findgameobjectwithtag ("Player"); AOffsetPos = This. transform.position-playerObject.transform.position; at transform. LookAt (playerobject.transform); - } - - - voidlateupdate () - { in //camera follow (using fixed offset method) - This. transform.position = OffsetPos +playerObject.transform.position; to //field of view stretching (using the Camera.fieldofview method) + Scrollofview (Cameraobject,fieldviewmin, Fieldviewmax, scrollspeed); - //field of view rotation (using the Transform.rotatearound method) theRotateofview (Cameraobject.transform, Playerobject.transform,refOffsetPos,refisrotate,rotatespeed); * } $ Panax Notoginseng - the + A /// <summary> the ///Wheel Control camera field stretching (mounted on camera) + /// </summary> - /// <param name= "_camera" >Camera</param> $ /// <param name= "_fieldviewmin" >Recent Horizons</param> $ /// <param name= "_fieldviewmax" >maximum field of view</param> - /// <param name= "_scrollspeed" >stretch Speed (default)</param> - voidScrollofview (Camera _camera,float_fieldviewmin,float_fieldviewmax,float_scrollspeed= +) the { -_camera.fieldofview-= Input.getaxis ("Mouse Scrollwheel") *_scrollspeed;Wuyi_camera.fieldofview =Mathf.clamp (_camera.fieldofview, _fieldviewmin, _fieldviewmax); the } - Wu - About $ /// <summary> - ///Right-drag rotation field of view (mounted on camera) - /// </summary> - /// <param name= "_cameratransform" >transform of the camera</param> A /// <param name= "_targettransform" >the transform of the target</param> + /// <param name= "_offsetpos" >offset position of camera and target (transitive)</param> the /// <param name= "_isrotate" >Rotation Status identifier (can be delivered by hand)</param> - /// <param name= "_rotatespeed" >rotation speed (default is ten)</param> $ voidRotateofview (Transform _cameratransform, Transform _targettransform,refVector3 _offsetpos,ref BOOL_isrotate,float_rotatespeed=Ten) the { the //when the right mouse button is pressed to toggle the rotation state, lift indicates the end rotation state the if(Input.getmousebuttondown (1)) the_isrotate =true; - if(Input.getmousebuttonup (1)) in_isrotate =false; the //Rotation State the if(_isrotate) About { theVector3 originalposition =_cameratransform.position; thequaternion originalrotation =_cameratransform.rotation; the_cameratransform.rotatearound (_targettransform.position, _targettransform.up, _rotatespeed * Input.GetAxis ("Mouse X")); +_cameratransform.rotatearound (_targettransform.position, _cameratransform.right,-_rotateSpeed * Input.GetAxis ("Mouse Y")); - //limit Upper and lower rotation angles the if(_cameratransform.eulerangles.x > the|| _cameratransform.eulerangles.x <Ten)Bayi { the_cameratransform.position =originalposition; the_cameratransform.rotation =originalrotation; - } - //Update offset the_offsetpos = _cameratransform.position-_targettransform.position; the } the } the -}
PlayerFollow.cs
Unity Learning-camera stretching, following, rotating