Test.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Test : MonoBehaviour
  5. {
  6. // Start is called before the first frame update
  7. public bool test_Robot_Movement;
  8. public bool test_Robot_AutoDrive;
  9. public GameObject autoDriveTarget;
  10. public bool autoDrive;
  11. public Vector2 signal;
  12. private GameObject[] robots;
  13. private void Start() {
  14. }
  15. // Update is called once per frame
  16. void Update()
  17. {
  18. if(robots == null || robots.Length==0) robots = GameObject.FindGameObjectsWithTag("robot");
  19. foreach (GameObject robot in robots){
  20. if(test_Robot_Movement) move(robot);
  21. if(test_Robot_AutoDrive){
  22. startAutoDrive(robot);
  23. Vector3 linearVelocity = new Vector3(0,0,signal.y);
  24. Vector3 angularVelocity = new Vector3(0,signal.x,0);
  25. robot.transform.Translate(linearVelocity);
  26. robot.transform.Rotate(angularVelocity);
  27. }
  28. }
  29. }
  30. // move the robot in unity
  31. private void move(GameObject robot){
  32. RoboterControl robotControl = robot.GetComponent<RoboterControl>();
  33. if(robotControl != null && robotControl.enabled){
  34. Vector2 controlSignal = robot.GetComponent<RoboterControl>().getControlSignal();
  35. Vector3 linearVelocity = new Vector3(0,0,controlSignal.y);
  36. Vector3 angularVelocity = new Vector3(0,controlSignal.x,0);
  37. robot.transform.Translate(linearVelocity);
  38. robot.transform.Rotate(angularVelocity);
  39. }
  40. else{
  41. Vector2 controlSignal = GameObject.Find("Input").GetComponent<VRInput>().getTouchValue();
  42. Vector3 linearVelocity = new Vector3(0,0,controlSignal.y);
  43. Vector3 angularVelocity = new Vector3(0,controlSignal.x,0);
  44. robot.transform.Translate(linearVelocity);
  45. robot.transform.Rotate(angularVelocity);
  46. }
  47. }
  48. private void startAutoDrive(GameObject robot){
  49. float distance = Vector3.Distance(autoDriveTarget.transform.position, robot.transform.position);
  50. if(distance < 1){
  51. signal = new Vector2(0,0);
  52. autoDrive = false;
  53. }
  54. if(autoDrive){
  55. float angleDiff = Vector3.Angle( autoDriveTarget.transform.position - robot.transform.position, robot.transform.forward);
  56. if(angleDiff > 1){
  57. Vector3 normal = Vector3.Cross(autoDriveTarget.transform.position, robot.transform.position);
  58. float direction = Mathf.Sign (Vector3.Dot(normal,robot.transform.forward));
  59. signal = new Vector2(direction * 0.2f, 0.01f * (180-angleDiff)/180);
  60. }
  61. else{
  62. //signal = new Vector2(0,velocitySpeed);
  63. signal = new Vector2(0,0.1f);
  64. }
  65. }
  66. }
  67. }