Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize camera Control by unity

Shulou Source: shulou.com Published: 2022-06-02 04:11:03 10月01日 Update

This article is about how unity implements camera control. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The first and third person controllers in unity all have a script called Mouse Look, which I wrote down.

[AddComponentMenu ("Camera-Control/Mouse Look")] public enum RotationAxes {MouseXAndY=0,Mouse1,MouseY=2} public RotationAxes axes=RotationAxes.MouseXAndY;public float SensitivityX=15F;public float SensitivityY=15F;public float minimumX=-360F;public float maxmumX=360F;public float minimumY=-60F;public float maxmumY=60F;float rotationY=0F;void start () {if (Getcomponent ()) {GetComponent (). FreezeRotation=true;}} void update () {if (axes==RotationAxes.MouseXAndY) {float rotationX=transform.localEulerAngles.y+Input.GetAxis ("MouseX") * SensitivityX RotationY+=Input.GetAxis ("MouseY") * SensitivityY; rotationY=Mathf.Clamp (rotationY,minimumY,maxmumY); transform.localEulerAngles=new Vector3 (- rotationY,rotationX,0);} else if (axes==RotationAxes.MouseX) {transform.Rotate (0Magnum Input.GetAxis ("MouseX") * SensitivityX,0);} else {rotationY+=Input.GetAxis ("MouseY") * SensitivityY; rotationY=Mathf.Clamp (rotationY,minimumY,maxmumY) Transform.localEulerAngles=new Vector3 (- rotationY,transform.localEulerAngles.y,0);}}

The protagonist faces the direction of the mouse click:

Vector3 MousePositionMousePositionObtains the screen coordinates of the mouse; Vector3 p_pos=Camera.main.WorldToScreenPoint (transform.position); / / converts the protagonist's world coordinates to screen coordinates in order to obtain his z-axis coordinates; mroompos.z; / / assigns the screen z-axis coordinates of the protagonist to the mouse Vector3 wolrd; wolrd.x=Camera.main.ScreenToWorldPoint (m_pos) .x; world.z=Camera.main.ScreenToWorldPoint (m_pos) .z; world.y=transform.position.y / / y axis coordinates remain unchanged so that the protagonist will not go underground; transform.LookAt (world); / / the protagonist looks at the position of the mouse click

Slide the mouse to adjust the distance of the field of view:

Float fov=Camera.main.FieldOfView; / / get the value of the camera's adjusted field of view; fov+=Input.GetAxis ("Mouse ScrollWheel") * sensitivity; / / sliding built to adjust the field of view worth size; fov=Mathf.Clamp (fov,minFov,maxFov); / / limit the field of view value to (minFov,maxFov); Camera.main.FieldOfView=fov; / / then assign the adjusted value to the camera's field of view; thank you for reading! This is the end of the article on "how to achieve camera control in unity". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Coordinates protagonist field of view mouse camera camera adjustment control screen content size more article good practical world person location underground controller Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Xiaomi Shulou Tech Info Microsoft MySQL