You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CozmoMovement.cs 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CozmoMovement : MonoBehaviour
  5. {
  6. public float m_Speed = 12f; // How fast the tank moves forward and back.
  7. public float m_TurnSpeed = 180f; // How fast the tank turns in degrees per second.
  8. private string m_MovementAxisName; // The name of the input axis for moving forward and back.
  9. private string m_TurnAxisName; // The name of the input axis for turning.
  10. private Rigidbody m_Rigidbody; // Reference used to move the tank.
  11. private float m_MovementInputValue; // The current value of the movement input.
  12. private float m_TurnInputValue; // The current value of the turn input.
  13. private void Awake()
  14. {
  15. m_Rigidbody = GetComponent<Rigidbody>();
  16. }
  17. private void OnEnable()
  18. {
  19. // When the cozmo is turned on, make sure it's not kinematic.
  20. m_Rigidbody.isKinematic = false;
  21. // Also reset the input values.
  22. m_MovementInputValue = 0f;
  23. m_TurnInputValue = 0f;
  24. }
  25. private void OnDisable()
  26. {
  27. // When the cozmo is turned off, set it to kinematic so it stops moving.
  28. m_Rigidbody.isKinematic = true;
  29. }
  30. private void Start()
  31. {
  32. m_MovementAxisName = "Vertical";
  33. m_TurnAxisName = "Horizontal";
  34. }
  35. private void Update()
  36. {
  37. // Store the value of both input axes.
  38. m_MovementInputValue = Input.GetAxis(m_MovementAxisName);
  39. m_TurnInputValue = Input.GetAxis(m_TurnAxisName);
  40. }
  41. private void FixedUpdate()
  42. {
  43. // Adjust the rigidbodies position and orientation in FixedUpdate.
  44. Move();
  45. Turn();
  46. }
  47. public void Move(float directionValue)
  48. {
  49. // Create a vector in the direction the cozmo is facing with a magnitude based on the input, speed and the time between frames.
  50. Vector3 movement = directionValue * transform.forward * m_Speed * Time.deltaTime;
  51. // Apply this movement to the rigidbody's position.
  52. m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
  53. }
  54. public void Move()
  55. {
  56. Move(m_MovementInputValue);
  57. }
  58. //public void Move(float[] act)
  59. //{
  60. //}
  61. public void Turn()
  62. {
  63. Turn(m_TurnInputValue);
  64. }
  65. public void Turn(float turnValue)
  66. {
  67. // Determine the number of degrees to be turned based on the input, speed and time between frames.
  68. float turn = turnValue * m_TurnSpeed * Time.deltaTime;
  69. // Make this into a rotation in the y axis.
  70. Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
  71. // Apply this rotation to the rigidbody's rotation.
  72. m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);
  73. }
  74. }