// Update is called once per frame void Update() { if (Input.GetKeyUp(KeyCode.K)) { start = !start; } if (start) { if (user.transform.position.x > transform.position.x) { c2.Move(300, 300, 0); } else { c2.Move(-300, 300, 0); } if (Mathf.Abs(transform.position.x - user.transform.position.x) < 0.1) { c2.Stop(); controll.Rotation(straight_wall.transform.forward, this.gameObject, 0, 12, 200, true); } } }
// Update is called once per frame void Update() { anim = gameObject.GetComponent <Animator>(); print(gameObject.name + "target =" + target.name + target.transform.parent.name); if (target == null) { print(this.name + "no target"); } else { //print(this.name + "target =" + target.name + target.transform.parent.name); //print("avoidance = " + avoidance.gameObject.name); avoidance.SetDetectDirection(target); //print("start = " + start); bool state = !start_translate && !start_rotate; //print("state =" + state); //when the wall enter in_tuen area, set the in_turn true to prepare rotating //in_turn = In_turn.GetIn_turn(); bool trigger = EdgeTrigger(in_turn); if (in_turn && trigger) { //if (In_turn.GetWall() == target.transform.GetChild(0).gameObject) start_rotate = true; //print("trigger up"); } //when the target position changed float distance = (last_target_pos - target.transform.position).magnitude;//distance between last target and current target last_target_pos = target.transform.position; //*************** float err_dis = 0.001f; if (anim == null) { print("no animator"); } else { if (anim.GetCurrentAnimatorStateInfo(0).IsName("Standby")) { err_dis = 0.005f; } else { err_dis = 0.001f; } } //***************** if (distance > err_dis /*|| !in_turn*/) { //print("translate3:" + start_translate); start_translate = true; //print("translate4:" + start_translate); } //print("start =" + start); if (Input.GetKeyUp(KeyCode.K)) { switch_button = !switch_button; } Color color = Color.blue; Debug.DrawRay(target.transform.position, avoidance.GetAviodanceVector(), color, 0.1f, true); if (switch_button) { print("PID On"); if (start) { //print(this.name + "target =" + target.name); if (start_translate) { //print(gameObject.name + " translating"); /*if(in_turn) * controll.SetErrDistance(0.1f); * else * controll.SetErrDistance(0.3f);*/ //print("translate1:" + start_translate); //print("wallnum:" + wallnum); /*if(target = standBy) * start_translate = !controll.Translation(target.transform.position, wall, wallnum, true, p1, p2); * else*/ avoidTarget = target.transform.position + avoidance.GetAviodanceVector(); //Debug.DrawRay(avoidTarget, Vector3.forward * 1f, color,0.1f,true); //Debug.DrawRay(target.transform.position, avoidance.GetAviodanceVector(), color, 0.1f, true); if (robotic_wall_dir == Wall_Mov_Dir.Left_Right) { start_translate = !controll.Translation_LR(controll.FindRoomba(avoidTarget, target.transform.forward), wall, wallnum, true, p1, p2); } else { start_translate = !controll.Translation_FB(controll.FindRoomba(avoidTarget, target.transform.forward), wall, wallnum, true, p1, p2); } //print("translate2:" + start_translate); } } //********************************************** if (!start_translate /*&& start_rotate*/) { print("rotating"); double angle = AngleSigned(wall.transform.forward, target.transform.forward, Vector3.up); if (angle > -90 && angle < 90) { start_rotate = !controll.Rotation(target.transform.forward, wall, wallnum, p, d_rotate, true); } else { start_rotate = !controll.Rotation(-target.transform.forward, wall, wallnum, p, d_rotate, true); } } } } }