In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.