|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- ///-----------------------------------------------------------------
- /// Namespace: <Cozmo>
- /// Class: <CozmoMovement>
- /// Description: <Defines the possible movement of the virtual cozmo.>
- /// Author: <Tobias Hassel> 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<Rigidbody>();
- }
-
-
- 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();
- }
-
- /// <summary>
- /// Move the cozmo forward (0 < directionValue <= 1) or backwards (0 > directionvalue >= -1)
- /// </summary>
- /// <param name="directionValue"></param>
- 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);
- }
-
- /// <summary>
- /// Move cozmo based on the input value
- /// </summary>
- public void Move()
- {
- Move(m_MovementInputValue);
- }
-
-
- /// <summary>
- /// Rotate cozmo based on the input value
- /// </summary>
- public void Turn()
- {
- Turn(m_TurnInputValue);
- }
-
- /// <summary>
- /// Rotate cozmo
- /// </summary>
- /// <param name="turnValue"></param>
- 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);
- }
-
- }
- }
|