UnityMouseLookコードの詳細



Unity Mouselook Code Details



ちなみに私が最近やっているFPSはたまたま説明されたばかりです。

using UnityEngine using System.Collections // MouseLook rotates the transform based on the mouse delta. // To make an FPS style character: // - Create a capsule. // - Add the MouseLook script to the capsule. // -> Set the mouse look to use MouseX. (You want to only turn character but not tilt it) // - Add FPSInput script to the capsule // -> A CharacterController component will be automatically added. // // - Create a camera. Make the camera a child of the capsule. Position in the head and reset the rotation. // - Add a MouseLook script to the camera. // -> Set the mouse look to use MouseY. (You want the camera to tilt up and down like a head. The character already turns.) [AddComponentMenu('Control Script/Mouse Look')]//Set the script path public class MouseLook : MonoBehaviour { public enum RotationAxes { // enumeration axes MouseXAndY = 0, MouseX = 1, MouseY = 2 } Public RotationAxes axes = RotationAxes.MouseXAndY //Define the X and Y axes RotationAxes as the rotation axis Public float sensitivityHor = 9.0f// horizontal sensitivity Public float sensitivityVert = 9.0f// flip sensitivity Public float minimumVert = -45.0f//Set the maximum and minimum of the flip angle public float maximumVert = 45.0f Private float _rotationX = 0 / / x axis rotation angle void Start() { / / Make the rigid body does not change the rotation Rigidbody body = GetComponent() if (body != null) body.freezeRotation = true//freeze rotation } void Update() { If (axes == RotationAxes.MouseX) / / determine the rotation axis is the X or Y axis { transform.Rotate(0, Input.GetAxis('Mouse X') * sensitivityHor, 0)//Rotate around the X axis Multiply by the corresponding sensitivity } else if (axes == RotationAxes.MouseY) { _rotationX -= Input.GetAxis('Mouse Y') * sensitivityVert _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert)//Mathf.Clamp : limits the value of value between min and max, // If the value is less than min, return min. Returns max if value is greater than max, otherwise returns value transform.localEulerAngles = new Vector3(_rotationX, transform.localEulerAngles.y, 0)//localEulerAngles own Euler angles, // Say this is the rotation angle of the object itself relative to the parent object, which can be used to read and set the angle. Can not be used for incrementing, angles over 360 degrees will be invalid. } else { float rotationY = transform.localEulerAngles.y + Input.GetAxis('Mouse X') * sensitivityHor _rotationX -= Input.GetAxis('Mouse Y') * sensitivityVert _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert) transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0) } } }