private void Turn() { SteamVR_Controller.Device device = SteamVR_Controller.Input((int)track.index); bool isPressTouchPad = device.GetPress(SteamVR_Controller.ButtonMask.Touchpad); float angle = 0f; if (isPressTouchPad) { Vector2 touchPos = device.GetAxis(); angle = Vector2.Angle(new Vector2(0f, 1f), touchPos); Vector3 cross = Vector3.Cross(new Vector2(0f, 1f), touchPos); angle = cross.z > 0 ? -angle : angle; } //监听用户按键事件W if (Input.GetKey(KeyCode.W) || (isPressTouchPad && (angle >= -45f && angle <= 45f))) { _NextDir = HeadDir.Up; if (_CurrentDir == HeadDir.Down) { _NextDir = _CurrentDir; } } if (Input.GetKey(KeyCode.S) || (isPressTouchPad && (angle <= -135f || angle >= 135f))) { _NextDir = HeadDir.Down; if (_CurrentDir == HeadDir.Up) { _NextDir = _CurrentDir; } } if (Input.GetKey(KeyCode.A) || (isPressTouchPad && (angle <= -45f && angle >= -135f))) { _NextDir = HeadDir.Left; if (_CurrentDir == HeadDir.Right) { _NextDir = _CurrentDir; } } if (Input.GetKey(KeyCode.D) || (isPressTouchPad && (angle >= 45f && angle <= 135f))) { _NextDir = HeadDir.Right; if (_CurrentDir == HeadDir.Left) { _NextDir = _CurrentDir; } } }
//控制蛇头移动 private void Move() { //将计时器累加时间增量 _Timer += Time.deltaTime; if (_Timer >= (1f / speed)) { //使蛇头旋转 switch (_NextDir) { case HeadDir.Up: transform.forward = Vector3.forward; _CurrentDir = HeadDir.Up; break; case HeadDir.Down: transform.forward = Vector3.back; _CurrentDir = HeadDir.Down; break; case HeadDir.Left: transform.forward = Vector3.left; _CurrentDir = HeadDir.Left; break; case HeadDir.Right: transform.forward = Vector3.right; _CurrentDir = HeadDir.Right; break; } //记录头部移动之前的位置 Vector3 nextPos = transform.position; //向前移动一个单位 transform.Translate(Vector3.forward); //重置计时器 _Timer = 0f; //如果有身子 if (_FirstBody != null) { //就让第一节身子移动 _FirstBody.MOve(nextPos); } } }
private void Move() { _Timer += Time.deltaTime; if (_Timer >= (1f / speed)) { //让蛇头旋转 switch (_NextDir) { case HeadDir.Up: transform.forward = Vector3.forward; _CurrentDir = HeadDir.Up; break; case HeadDir.Down: transform.forward = Vector3.back; _CurrentDir = HeadDir.Down; break; case HeadDir.Left: transform.forward = Vector3.left; _CurrentDir = HeadDir.Left; break; case HeadDir.Right: transform.forward = Vector3.right; _CurrentDir = HeadDir.Right; break; } //纪录头部移动之前的位置 Vector3 nextPos = transform.position; //移动头部 transform.Translate(Vector3.forward); if (_FirstBody != null) { _FirstBody.Move(nextPos); } //重置计时器 _Timer = 0f; } }
//控制蛇头转向 private void Turn() { //监听用户事件,上 if (Input.GetKey(KeyCode.W)) { _NextDir = HeadDir.Up; if (_CurrentDir == HeadDir.Down) { _NextDir = _CurrentDir; } } //监听用户事件,下 if (Input.GetKey(KeyCode.S)) { _NextDir = HeadDir.Down; if (_CurrentDir == HeadDir.Up) { _NextDir = _CurrentDir; } } //监听用户事件,左 if (Input.GetKey(KeyCode.A)) { _NextDir = HeadDir.Left; if (_CurrentDir == HeadDir.Right) { _NextDir = _CurrentDir; } } //监听用户事件,右 if (Input.GetKey(KeyCode.D)) { _NextDir = HeadDir.Right; if (_CurrentDir == HeadDir.Left) { _NextDir = _CurrentDir; } } }