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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ///-----------------------------------------------------------------
  2. /// Namespace: <Cozmo>
  3. /// Class: <CozmoMovement>
  4. /// Description: <Defines the possible movement of the virtual cozmo.>
  5. /// Author: <Tobias Hassel> Date: <29.07.2019>
  6. /// Notes: <>
  7. ///-----------------------------------------------------------------
  8. ///
  9. using UnityEngine;
  10. namespace Cozmo
  11. {
  12. public class CozmoMovement : MonoBehaviour
  13. {
  14. private const string MOVEMENT_AXIS_NAME = "Vertical"; // name of the movement input axis
  15. private const string TURN_AXIS_NAME = "Horizontal"; // name of the rotation input axis
  16. [Tooltip("Determines how fast the robot is moving forward and backwards.")]
  17. public float m_Speed = 12f;
  18. [Tooltip("Determines how fast the robot is turning in degrees per second.")]
  19. public float m_TurnSpeed = 180f;
  20. private Rigidbody cozmoRigidbody; // Reference to cozmos rigidbody
  21. private float m_MovementInputValue; // current input value for movement
  22. private float m_TurnInputValue; // current input value for rotation
  23. private void Awake()
  24. {
  25. cozmoRigidbody = GetComponent<Rigidbody>();
  26. }
  27. private void OnEnable()
  28. {
  29. // When the cozmo is turned on, make sure it's not kinematic.
  30. cozmoRigidbody.isKinematic = false;
  31. // reset movement and rotation values
  32. m_MovementInputValue = 0f;
  33. m_TurnInputValue = 0f;
  34. }
  35. private void OnDisable()
  36. {
  37. // When the cozmo is turned off, set it to kinematic so it stops moving.
  38. cozmoRigidbody.isKinematic = true;
  39. }
  40. private void Update()
  41. {
  42. // Store the value of both input axes.
  43. m_MovementInputValue = Input.GetAxis(MOVEMENT_AXIS_NAME);
  44. m_TurnInputValue = Input.GetAxis(TURN_AXIS_NAME);
  45. }
  46. private void FixedUpdate()
  47. {
  48. // Adjust position and rotation
  49. Move();
  50. Turn();
  51. }
  52. /// <summary>
  53. /// Move the cozmo forward (0 < directionValue <= 1) or backwards (0 > directionvalue >= -1)
  54. /// </summary>
  55. /// <param name="directionValue"></param>
  56. public void Move(float directionValue)
  57. {
  58. // clamp directionValue to make sure its between -1 and 1
  59. Mathf.Clamp(directionValue, -1, 1);
  60. Vector3 movement = directionValue * transform.forward * m_Speed * Time.deltaTime;
  61. // Apply this movement to the rigidbody's position.
  62. cozmoRigidbody.MovePosition(cozmoRigidbody.position + movement);
  63. }
  64. /// <summary>
  65. /// Move cozmo based on the input value
  66. /// </summary>
  67. public void Move()
  68. {
  69. Move(m_MovementInputValue);
  70. }
  71. /// <summary>
  72. /// Rotate cozmo based on the input value
  73. /// </summary>
  74. public void Turn()
  75. {
  76. Turn(m_TurnInputValue);
  77. }
  78. /// <summary>
  79. /// Rotate cozmo
  80. /// </summary>
  81. /// <param name="turnValue"></param>
  82. public void Turn(float turnValue)
  83. {
  84. // clamp turnValue to make sure its between -1 and 1
  85. Mathf.Clamp(turnValue, -1, 1);
  86. float turn = turnValue * m_TurnSpeed * Time.deltaTime;
  87. // Make this into a rotation in the y axis.
  88. Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
  89. // Apply this rotation to the rigidbody's rotation.
  90. cozmoRigidbody.MoveRotation(cozmoRigidbody.rotation * turnRotation);
  91. }
  92. }
  93. }