void Update() { if (!ControlsEnabled) { return; } #region get the command //读取字节------------------这里有一个大坑:Python写内存是按字节写入的,所以读内存的时候一定要按字节读出,否则不正确 String com = ""; for (int i = 0; i <= 5; i++) { byte ReadByte = viewAccessor.ReadByte(i); command[i] = ReadByte - '0'; //print(ReadByte); //print(command[i]); com += command[i].ToString(); } print(com); #endregion #region grab objects //0:没有命令 --》按空格抓取 --》1:翻转将手心向下 --》2:伸手(把手运动到目标位置) --》 3:弯曲手指抓 --》4:把手收回来(收到按下空格时的位置)--》5:把手翻过 --》6:等待放下物体--》7:把手翻到0度(手心向下)--》8:松开手 //print(Controller.Parts.Fingers[1].IsBending); //获取以坐标GPS_transform.position(手掌的中心)为圆心,以DetectionRadius为半径的球的范围内的Layer为‘player’的物体 Collider[] cs = Physics.OverlapSphere(GPS_transform.position, DetectionRadius, 1 << LayerMask.NameToLayer("player")); if (cs.Length != 0 && grabStep == 0) { //print("cube1: " + cube1.GetComponent<Transform>().position + "cube2: " + cube2.GetComponent<Transform>().position + "capsule: " + capsule.GetComponent<Transform>().position); handStartGrabPosition = Controller.Parts.Forearm.transform.position; handTargetPosition = Vector3.zero; if (pick_player)//把上一个被检测成红色的物体转换为蓝色 { pick_player.GetComponent <MeshRenderer>().material.color = Color.blue; } pick_player = cs[0].gameObject; //print("pick_player: " + pick_player.name); pick_player.GetComponent <Rigidbody>().useGravity = false; pick_player_transform = pick_player.GetComponent <Transform>(); pick_player.GetComponent <MeshRenderer>().material.color = Color.red; Vector3 tra_vec = pick_player_transform.position - GPS_transform.position; //print(handTargetPosition); handTargetPosition = new Vector3( Controller.Parts.Forearm.transform.position.x + tra_vec.x, Controller.Parts.Forearm.transform.position.y + tra_vec.y + pick_player_transform.position.y, Controller.Parts.Forearm.transform.position.z + tra_vec.z); //print( // "pick_player_name: " + pick_player.name + // " Forearm.transform: " + Controller.Parts.Forearm.transform.position + // " GPS_transform: " + GPS_transform.position + // " pick_player: " + pick_player_transform.position + // " tra_vec: " + tra_vec + // " handTargetPosition: " + handTargetPosition); } else if (grabStep == 0)//没有检测到物体,状态为0,(没有发出命令) { if (pick_player) { pick_player.GetComponent <MeshRenderer>().material.color = Color.blue; } pick_player = null; } if (Input.GetKey(pickup) && grabStep == 0 && pick_player)//按空格 { resetTargetRotation(); grabStep = 1; } if (grabStep == 1) { Controller.RotateForearmTo(0);//先将手转到0度,手心向下为0度 wait(15, 2); } if (grabStep == 2) //伸手去抓 { Controller.Parts.Forearm.transform.position = Vector3.MoveTowards(Controller.Parts.Forearm.transform.position, handTargetPosition, step); if (Mathf.Approximately(Controller.Parts.Forearm.transform.position.y, handTargetPosition.y)) { grabStep = 3; Controller.StartBendFingers(); } } if (grabStep == 3) //抓 { wait(30, 4); //等待一段时间后再转到状态3 } if (grabStep == 4)//收回手 { Controller.Parts.Forearm.transform.position = Vector3.MoveTowards(Controller.Parts.Forearm.transform.position, handStartGrabPosition, step); if (Mathf.Approximately(Controller.Parts.Forearm.transform.position.y, handStartGrabPosition.y)) { grabStep = 5; } } if (grabStep == 5)//翻手 { Controller.RotateForearm(-160); wait(30, 6);//等待一段时间后,转到状态6 } if (Input.GetKey(pickup) && grabStep == 6) { grabStep = 7;//如果再次按下空格键,则转到状态7, } if (grabStep == 7) { Controller.RotateForearmTo(0);//状态7进行翻手,翻到0度,掌心向下为0度 wait(30, 8); } if (grabStep == 8) { Controller.StopBendFingers();//翻手之后,松开物体 wait(30, 0); } if (Input.GetKey(Grab))//按‘G’抓 { grabStep = 99; resetTargetRotation(); Controller.StartBendFingers(); } if (Input.GetKey(StopGrab))//按‘F’松开 { grabStep = 0; Controller.StopBendFingers(); } #endregion #region gestures if (Input.GetKey(KeyCode.Alpha1)) { grabStep = 99; resetTargetRotation(); Controller.StartBendFinger(FingerType.Middle); Controller.StartBendFinger(FingerType.Ring); Controller.StartBendFinger(FingerType.Pinky); } if (Input.GetKey(KeyCode.Alpha2)) { grabStep = 99; resetTargetRotation(); Controller.StartBendFinger(FingerType.Ring); Controller.StartBendFinger(FingerType.Pinky); } if (Input.GetKey(KeyCode.Alpha3)) { grabStep = 99; resetTargetRotation(); Controller.StartBendFinger(FingerType.Index); } #endregion if (Input.GetKey(KeyCode.Q)) { grabStep = 99; StartBendFinger(1, 0.0f, 0.3f, 0.5f); } if (Input.GetKey(KeyCode.E)) { grabStep = 99; StartBendFinger(1, 0.0f, 0.8f, 0.5f); } //if (Input.GetKey(KeyCode.Y)) //{ // Controller.RotateWristTo(45); //} #region controll by command if (grabStep == 0) { if (command[0] == 1) { StartBendFinger(0, 0.0f, 0.0f, 0.3f);//i指的是第i个手指,后边三个浮点数参数为每个关节的弯曲程度,1是最大弯曲 } else if (command[0] == 2) { StartBendFinger(0, 0.0f, 0.0f, 0.9f); } else if (command[0] == 0) { StopBendFinger(0); } for (int i = 1; i < 5; i++) { //command[i]为1,2,3分别为三种弯曲程度 if (command[i] == 1) { StartBendFinger(i, 0.0f, 0.3f, 0.5f);//i指的是第i个手指,后边三个浮点数参数为每个关节的弯曲程度,1是最大弯曲 } else if (command[i] == 2) { StartBendFinger(i, 0.0f, 0.6f, 0.5f); } else if (command[i] == 3) { StartBendFinger(i, 0.0f, 0.9f, 0.5f); } else if (command[i] == 0) { StopBendFinger(i); } } if (command[5] == 1) //用command[5]控制手腕弯曲的角度 { Controller.RotateWristTo(25); //控制手腕弯曲的角度:25度 } else if (command[5] == 2) { Controller.RotateWristTo(45); } else if (command[5] == 0) { Controller.RotateWristTo(0); } } #endregion if (!Input.GetKey(HoldRotation)) { Controller.MoveForearm(new Vector3(Input.GetAxis(MoveForearmAxisX), Input.GetAxis(MoveForearmAxisY), Input.GetAxis(MoveForearmAxisZ))); } else { Controller.RotateWrist(Input.GetAxis(RotateWristAxis)); Controller.RotateForearm(Input.GetAxis(RotateForearmAxis)); } }
void Update() { //Enable or disable control if (Input.GetKeyDown(KeyCode.C)) { _handController.EnableControl = !_handController.EnableControl; } //Bend or unbend all fingers if (Input.GetMouseButtonDown(0)) { _handController.BendAllFingers(); } if (Input.GetMouseButtonUp(0)) { _handController.UnbendAllFingers(); } //Bend or unbend one specific finger if (Input.GetKeyDown(KeyCode.A)) { _handController.BendFinger(FingersType.Pinky); } if (Input.GetKeyDown(KeyCode.S)) { _handController.BendFinger(FingersType.Ring); } if (Input.GetKeyDown(KeyCode.D)) { _handController.BendFinger(FingersType.Middle); } if (Input.GetKeyDown(KeyCode.F)) { _handController.BendFinger(FingersType.Index); } if (Input.GetKeyDown(KeyCode.Space)) { _handController.BendFinger(FingersType.Thumb); } if (Input.GetKeyUp(KeyCode.A)) { _handController.UnbendFinger(FingersType.Pinky); } if (Input.GetKeyUp(KeyCode.S)) { _handController.UnbendFinger(FingersType.Ring); } if (Input.GetKeyUp(KeyCode.D)) { _handController.UnbendFinger(FingersType.Middle); } if (Input.GetKeyUp(KeyCode.F)) { _handController.UnbendFinger(FingersType.Index); } if (Input.GetKeyUp(KeyCode.Space)) { _handController.UnbendFinger(FingersType.Thumb); } //Change hand type // if (Input.GetKeyUp(KeyCode.G)) // { // if (_handController.HandType == HandTyp.LeftHand) // _handController.ChangeHandType(HandTyp.RightHand); // else _handController.ChangeHandType(HandTyp.LeftHand); // } //Rotate forearm and wrist if (Input.GetMouseButton(1)) { _handController.RotateForearm(Input.GetAxis("Mouse X")); _handController.RotateWrist(Input.GetAxis("Mouse Y")); } //Move forearm else { _handController.MoveForearm(new Vector3(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse ScrollWheel"), Input.GetAxis("Mouse Y"))); } Screen.lockCursor = true; }