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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 tank 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 tank is turned off, set it to kinematic so it stops moving.
  28. m_Rigidbody.isKinematic = true;
  29. }
  30. private void Start()
  31. {
  32. // The axes names are based on player number.
  33. m_MovementAxisName = "Vertical";
  34. m_TurnAxisName = "Horizontal";
  35. }
  36. private void Update()
  37. {
  38. // Store the value of both input axes.
  39. m_MovementInputValue = Input.GetAxis(m_MovementAxisName);
  40. m_TurnInputValue = Input.GetAxis(m_TurnAxisName);
  41. }
  42. private void FixedUpdate()
  43. {
  44. // Adjust the rigidbodies position and orientation in FixedUpdate.
  45. Move();
  46. Turn();
  47. }
  48. public void Move(float directionValue)
  49. {
  50. // Create a vector in the direction the tank is facing with a magnitude based on the input, speed and the time between frames.
  51. Vector3 movement = directionValue * transform.forward * m_Speed * Time.deltaTime;
  52. // Apply this movement to the rigidbody's position.
  53. m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
  54. }
  55. public void Move()
  56. {
  57. Move(m_MovementInputValue);
  58. }
  59. //public void Move(float[] act)
  60. //{
  61. //}
  62. public void Turn()
  63. {
  64. Turn(m_TurnInputValue);
  65. }
  66. public void Turn(float turnValue)
  67. {
  68. // Determine the number of degrees to be turned based on the input, speed and time between frames.
  69. float turn = turnValue * m_TurnSpeed * Time.deltaTime;
  70. // Make this into a rotation in the y axis.
  71. Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
  72. // Apply this rotation to the rigidbody's rotation.
  73. m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);
  74. }
  75. }