private void MovePlayer(Connection connection) { Rigidbody rb = _connectionToRigidbody[connection]; float horizontalInput = 0; float verticalInput = 0; if (NetworkInput.GetKey(connection, Keys.MoveLeft)) { horizontalInput = -1; } if (NetworkInput.GetKey(connection, Keys.MoveRight)) { horizontalInput = 1; } if (NetworkInput.GetKey(connection, Keys.MoveForward)) { verticalInput = 1; } if (NetworkInput.GetKey(connection, Keys.MoveBackward)) { verticalInput = -1; } rb.AddForce(new Vector3(horizontalInput * setup.moveInputIntensity, 0, verticalInput * setup.moveInputIntensity)); if (NetworkInput.GetKey(connection, Keys.Pull)) { Vector3 pos = new Vector3(rb.transform.position.x, -1, rb.transform.position.z); Collider[] hitColliders = Physics.OverlapSphere(pos, 5); int i = 0; while (i < hitColliders.Length) { Rigidbody colliderRb = hitColliders[i].GetComponent <Rigidbody>(); if (colliderRb != null && colliderRb != rb) { colliderRb.AddExplosionForce(50f, pos, 5f); } i++; } } if (NetworkInput.GetKey(connection, Keys.Push)) { Vector3 pos = new Vector3(rb.transform.position.x, -1, rb.transform.position.z); Collider[] hitColliders = Physics.OverlapSphere(pos, 5); int i = 0; while (i < hitColliders.Length) { Rigidbody colliderRb = hitColliders[i].GetComponent <Rigidbody>(); if (colliderRb != null && colliderRb != rb) { colliderRb.AddForce((rb.transform.position - hitColliders[i].transform.position) * 10f); } i++; } } }
void Update() { //setup code to get initial rotation if (Input.GetKeyDown(KeyCode.Tab)) { got_first = false; } //Used to time the robot, how long it took to reach the destination if (isTimer) { timeForTable += Time.deltaTime; } /* * Following code takes in Inputs WASD from user and moves the robot depending on the input. * W: Forward movement * S: Backward movement * A: Left rotation * D: Right rotation */ if (Input.GetKey(KeyCode.W)) { controllerValue1 = maxSpeed; controllerValue2 = maxSpeed; controllerValue3 = maxSpeed; controllerValue4 = maxSpeed; this.transform.position += transform.forward * moveSpeed * Time.deltaTime; } else if (Input.GetKey(KeyCode.S)) { controllerValue1 = -maxSpeed; controllerValue2 = -maxSpeed; controllerValue3 = -maxSpeed; controllerValue4 = -maxSpeed; this.transform.position -= transform.forward * moveSpeed * Time.deltaTime; } else if (Input.GetKey(KeyCode.A)) { controllerValue1 = -maxSpeed; controllerValue2 = maxSpeed; controllerValue3 = -maxSpeed; controllerValue4 = maxSpeed; if (simulate_rotate) { this.transform.Rotate(-Vector3.up * rotateSpeed * Time.deltaTime); } } else if (Input.GetKey(KeyCode.D)) { controllerValue1 = maxSpeed; controllerValue2 = -maxSpeed; controllerValue3 = maxSpeed; controllerValue4 = -maxSpeed; if (simulate_rotate) { this.transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime); } } else { controllerValue1 = 0f; controllerValue2 = 0f; controllerValue3 = 0f; controllerValue4 = 0f; } /* * Following code takes in Inputs WASD from AI &moves the robot depending on the input. * W: Forward movement * S: Backward movement * A: Left rotation * D: Right rotation * It also takes into account the current distance it is away from the destination and determines if * the movement taken is positive or negative toward the goal. */ if (NetworkInput.GetKey(KeyCode.W)) { controllerValue1 = maxSpeed; controllerValue2 = maxSpeed; controllerValue3 = maxSpeed; controllerValue4 = maxSpeed; //Distance taken from the moment before the movement is applied var distance = Vector3.Distance(this.transform.position, Playground.instance.destinationPos); this.transform.position += transform.forward * moveSpeed * Time.deltaTime; //toScore used to only allocate scores once at a time if (toScore) { //Check if the distance between the robot and Destination got larger or smaller if (Vector3.Distance(this.transform.position, Playground.instance.destinationPos) < distance) { //allocate score if distance was reduced score++; } else { //otherwise penalize the AI score -= 2; } toScore = false; } } else if (NetworkInput.GetKey(KeyCode.S)) { controllerValue1 = -maxSpeed; controllerValue2 = -maxSpeed; controllerValue3 = -maxSpeed; controllerValue4 = -maxSpeed; var distance = Vector3.Distance(this.transform.position, Playground.instance.destinationPos); this.transform.position -= transform.forward * moveSpeed * Time.deltaTime; if (toScore) { if (Vector3.Distance(this.transform.position, Playground.instance.destinationPos) < distance) { score++; } else { score -= 2; } toScore = false; } } else if (NetworkInput.GetKey(KeyCode.A)) { controllerValue1 = -maxSpeed; controllerValue2 = maxSpeed; controllerValue3 = -maxSpeed; controllerValue4 = maxSpeed; if (simulate_rotate) { this.transform.Rotate(-Vector3.up * rotateSpeed * Time.deltaTime); } if (toScore) { score--; toScore = false; } } else if (NetworkInput.GetKey(KeyCode.D)) { controllerValue1 = maxSpeed; controllerValue2 = -maxSpeed; controllerValue3 = maxSpeed; controllerValue4 = -maxSpeed; if (simulate_rotate) { this.transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime); } if (toScore) { score--; toScore = false; } } else { controllerValue1 = 0f; controllerValue2 = 0f; controllerValue3 = 0f; controllerValue4 = 0f; toScore = true; } SendRobotCommand((int)controllerValue1, (int)controllerValue2, (int)controllerValue3, (int)controllerValue4); }