Unity 3D Обертання камери за мишею

2075 / Unity / 3D / Обертання камери за мишею


public float sensitivity = 5.0f;
public float smoothing = 2.0f;
GameObject character;
Vector2 mouseLook; 
Vector2 smoothV;


void Start () {
  character = transform.parent.gameObject;
}

void Update () {
  var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
  md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
  smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
  smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
  mouseLook += smoothV;
  transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
  character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
}