【ZZAY】Unity 1.1 课堂笔记 + 开发进度(注释详细)
Unity 1.1.1 课堂笔记
-
[SerializeField]
注解:-
该注解用于成员变量,可以将原本不会被序列化的 private 变量和 protected 变量变成可以被序列化的。
-
由于 Unity 会自动对 public 变量执行序列化,而不对 private 变量执行序列化。而在 Unity 中,只有被序列化的变量才会显示在 Inspector 中。因此,如果希望在调试时实时修改某些变量的数值,可以在变量前添加该注解将其标记为序列化字段,而不需要修改其作用域。
-
-
[HideInInspector]
注解:-
该注解用于成员变量,可以将原本显示在面板上的序列化变量隐藏起来。
-
由于 Unity 会自动对 public 变量执行序列化,而不对 private 变量执行序列化。而在 Unity 中,只有被序列化的变量才会显示在 Inspector 窗口中。因此,如果不希望在 Inspector 中显示某些变量的数值,可以在对应变量前添加该注解将其隐藏,而不需要修改其作用域或者序列化属性。
-
-
FixedUpdate()
vs.Update()
:-
与
RigidBody
(刚体)相关的变化通常写在FixedUpdate()
中,例如模拟物体的物理轨迹。 -
通俗而言,可以认为
FixedUpdate()
能够保证一秒钟内所执行的各任务是严格均匀的(平滑),而Update()
不一定保证一秒钟内所执行的各任务是严格均匀的(不平滑)。
-
Unity 1.1.2 开发进度
-
应用截图:
-
PlayerInput
:该类主要用于处理用户输入。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
/// <summary>
/// <para>Handles the input for controlling the player.</para>
/// </summary>
public class PlayerInput : MonoBehaviour
{
// The speed of the player's movement
[SerializeField] private float moveSpeed = 4.0f;
// The speed of the camera's movement
[SerializeField] private float lookSensitivity = 12.0f;
// The instance of the player controller
[SerializeField] private PlayerController playerController;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
// Get the player's movement input, calculate the velocity, and move
float xMove = Input.GetAxisRaw("Horizontal");
float yMove = Input.GetAxisRaw("Vertical");
Vector3 velocity = (transform.right * xMove + transform.forward * yMove).normalized * moveSpeed;
playerController.Move(velocity);
// Get the cursor's movement input, calculate the rotation, and rotate
float xMouse = Input.GetAxisRaw("Mouse X");
float yMouse = Input.GetAxisRaw("Mouse Y");
Vector3 yRotation = new Vector3(0.0f, xMouse, 0.0f) * lookSensitivity;
Vector3 xRotation = new Vector3(-yMouse, 0.0f, 0.0f) * lookSensitivity;
playerController.Rotate(yRotation, xRotation);
}
}
PlayerController
:该类主要用于实现对玩家的操控。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// <para>Handles the control of the player, encompassing the player's attributes and actions.</para>
/// </summary>
public class PlayerController : MonoBehaviour
{
// The RigidBody instance of the player
[SerializeField] private Rigidbody rigidBody;
// The camera representing the player's perspective
[SerializeField] private Camera cam;
// The player's velocity
private Vector3 velocity = Vector3.zero;
// The player's physical rotation (left/right)
private Vector3 yRotation = Vector3.zero;
// The player's perspective rotation (up/down)
private Vector3 xRotation = Vector3.zero;
/// <summary>
/// <para>An interface for changing the player's velocity according to the input.</para>
/// </summary>
/// <param name="_velocity">The player's current velocity.</param>
public void Move(Vector3 _velocity)
{
velocity = _velocity;
}
/// <summary>
/// <para>An interface for changing the the player's and its perspective's rotations.</para>
/// </summary>
/// <param name="_yRotation">The player's physical rotation.</param>
/// <param name="_xRotation">The player's perspective rotation.</param>
public void Rotate(Vector3 _yRotation, Vector3 _xRotation)
{
yRotation = _yRotation;
xRotation = _xRotation;
}
/// <summary>
/// <para>Move the player.</para>
/// </summary>
private void PerformMovement()
{
if (velocity != Vector3.zero)
{
rigidBody.MovePosition(rigidBody.position + velocity * Time.fixedDeltaTime);
}
}
/// <summary>
/// <para>Rotate the player and its camera's perspective.</para>
/// </summary>
private void PerformRotation()
{
// Rotate the player (left/right)
if (yRotation != Vector3.zero)
{
rigidBody.transform.Rotate(yRotation);
}
// Rotate the camera's perspective (up/down)
if (xRotation != Vector3.zero)
{
cam.transform.Rotate(xRotation);
}
}
private void FixedUpdate()
{
PerformMovement();
PerformRotation();
}
}