///----------------------------------------------------------------- /// Namespace: /// Class: /// Description: /// Author: Date: <29.07.2019> /// Notes: <> ///----------------------------------------------------------------- /// using UnityEngine; namespace Cozmo { public class CozmoMovement : MonoBehaviour { private const string MOVEMENT_AXIS_NAME = "Vertical"; // name of the movement input axis private const string TURN_AXIS_NAME = "Horizontal"; // name of the rotation input axis [Tooltip("Determines how fast the robot is moving forward and backwards.")] public float m_Speed = 12f; [Tooltip("Determines how fast the robot is turning in degrees per second.")] public float m_TurnSpeed = 180f; private Rigidbody cozmoRigidbody; // Reference to cozmos rigidbody private float m_MovementInputValue; // current input value for movement private float m_TurnInputValue; // current input value for rotation private void Awake() { cozmoRigidbody = GetComponent(); } private void OnEnable() { // When the cozmo is turned on, make sure it's not kinematic. cozmoRigidbody.isKinematic = false; // reset movement and rotation values m_MovementInputValue = 0f; m_TurnInputValue = 0f; } private void OnDisable() { // When the cozmo is turned off, set it to kinematic so it stops moving. cozmoRigidbody.isKinematic = true; } private void Update() { // Store the value of both input axes. m_MovementInputValue = Input.GetAxis(MOVEMENT_AXIS_NAME); m_TurnInputValue = Input.GetAxis(TURN_AXIS_NAME); } private void FixedUpdate() { // Adjust position and rotation Move(); Turn(); } /// /// Move the cozmo forward (0 < directionValue <= 1) or backwards (0 > directionvalue >= -1) /// /// public void Move(float directionValue) { // clamp directionValue to make sure its between -1 and 1 Mathf.Clamp(directionValue, -1, 1); Vector3 movement = directionValue * transform.forward * m_Speed * Time.deltaTime; // Apply this movement to the rigidbody's position. cozmoRigidbody.MovePosition(cozmoRigidbody.position + movement); } /// /// Move cozmo based on the input value /// public void Move() { Move(m_MovementInputValue); } /// /// Rotate cozmo based on the input value /// public void Turn() { Turn(m_TurnInputValue); } /// /// Rotate cozmo /// /// public void Turn(float turnValue) { // clamp turnValue to make sure its between -1 and 1 Mathf.Clamp(turnValue, -1, 1); float turn = turnValue * m_TurnSpeed * Time.deltaTime; // Make this into a rotation in the y axis. Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f); // Apply this rotation to the rigidbody's rotation. cozmoRigidbody.MoveRotation(cozmoRigidbody.rotation * turnRotation); } } }