Unity3D review 1-objects follow the mouse to rotate


Preface

This is my review of playing Unity, some scripts to share


One, mouselook

1.1, for rotation

Considering that there are three different types of rotation behavior (horizontal rotation, vertical rotation, horizontal + vertical), which is equivalent to rotation including these three types, so use enumeration to define the three (enumeration type is a value type, it is used Declare a set of named constants)

public enum RorationAxes         //定义枚举数据结构,将名称和设置结合起来
    {
    
    
        MouseXAndY = 0,
        MouseX = 1,
        MouseY = 2   
    }

Declare a public variable of the enumeration, which will display the drop-down menu in the Inspector

public RorationAxes axes = RorationAxes.MouseXAndY;

1.2, horizontal rotation

void Update () {
    
    
        if (axes==RorationAxes.MouseX)
        {
    
    
            transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityHor, 0);
        }

1.3, vertical rotation

The Rotate() method can only be used to increase the rotation value without restrictions, suitable for horizontal rotation,
but vertical rotation needs to limit the degree of vertical tilt of the field of
view_rotationX-----rotation around the X axis vertically to maintain the angle

 public float sensitivityVert = 9.0f;//垂直转
    public float miniumVert = -45.0f;
    public float maxinumVert = 45.0f;
    public float _rotationX = 0;//为垂直角度声明一个角度变量,X代表X轴
 
//Mathf.Clamp用于将旋转角度保持在最小值和最大值之间
//Clamp()方法不止用于旋转,他通常用于确保一个数值变量在限制的范围内
else if (axes == RorationAxes.MouseY)
        {
    
    
            _rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
            _rotationX = Mathf.Clamp(_rotationX, miniumVert, maxinumVert);
            float rotationY = transform.localEulerAngles.y;
            transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);

 }

Since the angle property of transform is Vector3, you need to pass the rotation angle value to the constructor to create a new Vector3

Euler angle and Quaternion
refer to www.flipcode.com/documents/matrfaq.html-Q47

1.4, horizontal + vertical rotation

  else
        {
    
    
            _rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
            _rotationX = Mathf.Clamp(_rotationX, miniumVert, maxinumVert);
            float delat = Input.GetAxis("Mouse X") * sensitivityHor;//delat------是旋转的变化量
            float rotationY = transform.localEulerAngles.y + delat;
            transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
        }
    }
//使用delat递增旋转角度


 void Start()
    {
    
    
        Rigidbody body = GetComponent<Rigidbody>();
        if (body != null)
            body.freezeRotation = true;
 }

The code is as follows (example):

这段代码:
鼠标输入脚本通常在玩家的Rigidbody上设置freezeRotation属性。
 if (body != null)用于检测这个组件是否存在

2. Test results

The program was made with a Unity virtual reality book before.
Because I don’t just hold on to how to move the picture.
If you have any questions, please give me some advice, thank you!

Guess you like

Origin blog.csdn.net/CltCj/article/details/110748704