Я создаю контроллер от первого лица в Unity.Очень простая штука, камера - дитя капсулы Player.Коды работают, но мне нужна помощь, чтобы объяснить, что происходит.

* камера является дочерним элементом Player в иерархии

Вот мои вопросы:
Почему в PlayerMovement мы переводим по оси Z, чтобы добиться вертикального перемещения, когда Unity находится по оси Y-Up?
В CamRotation я понятия не имею, что происходит в Update()
.Почему мы применяем горизонтальное движение к нашему проигрывателю, а затем вертикальное движение к нашей камере?Почему он не применяется к тому же GameObject?
Чего добивается MouseMove?Почему мы используем var?
Я думаю, что мы получаем значение того, сколько мыши было перемещено, но что тогда делает с ним применение Vector2.Scale?
Код:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float speed = 5.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float mvX = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
float mvZ = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(mvX, 0, mvZ);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamRotation : MonoBehaviour {
public float horizontal_speed = 3.0F;
public float vertical_speed = 2.0F;
GameObject character; // refers to the parent object the camera is attached to (our Player capsule)
// initialization
void Start()
{
character = this.transform.parent.gameObject;
}
// Update is called once per frame
void Update()
{
var mouseMove = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
mouseMove = Vector2.Scale(mouseMove, new Vector2(horizontal_speed, vertical_speed));
character.transform.Rotate(0, mouseMove.x, 0); // to rotate our character horizontally
transform.Rotate(-mouseMove.y, 0, 0); // to rotate the camera vertically
}
}