2019-07-29 16:29:21 +02:00
|
|
|
|
///-----------------------------------------------------------------
|
|
|
|
|
/// Namespace: <Cozmo>
|
|
|
|
|
/// Class: <CozmoMovement>
|
|
|
|
|
/// Description: <Defines the possible movement of the virtual cozmo.>
|
|
|
|
|
/// Author: <Tobias Hassel> Date: <29.07.2019>
|
|
|
|
|
/// Notes: <>
|
|
|
|
|
///-----------------------------------------------------------------
|
|
|
|
|
///
|
2019-04-17 16:52:29 +02:00
|
|
|
|
|
|
|
|
|
|
2019-07-29 16:29:21 +02:00
|
|
|
|
using UnityEngine;
|
2019-04-17 16:52:29 +02:00
|
|
|
|
|
2019-07-29 16:29:21 +02:00
|
|
|
|
namespace Cozmo
|
|
|
|
|
{
|
|
|
|
|
public class CozmoMovement : MonoBehaviour
|
2019-04-17 16:52:29 +02:00
|
|
|
|
{
|
2019-07-29 16:29:21 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
2019-04-17 16:52:29 +02:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|