|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class CozmoMovement : MonoBehaviour
- {
-
- public float m_Speed = 12f; // How fast the tank moves forward and back.
- public float m_TurnSpeed = 180f; // How fast the tank turns in degrees per second.
-
-
- private string m_MovementAxisName; // The name of the input axis for moving forward and back.
- private string m_TurnAxisName; // The name of the input axis for turning.
- private Rigidbody m_Rigidbody; // Reference used to move the tank.
- private float m_MovementInputValue; // The current value of the movement input.
- private float m_TurnInputValue; // The current value of the turn input.
-
-
- private void Awake()
- {
- m_Rigidbody = GetComponent<Rigidbody>();
- }
-
-
- private void OnEnable()
- {
- // When the cozmo is turned on, make sure it's not kinematic.
- m_Rigidbody.isKinematic = false;
-
- // Also reset the input values.
- m_MovementInputValue = 0f;
- m_TurnInputValue = 0f;
- }
-
-
- private void OnDisable()
- {
- // When the cozmo is turned off, set it to kinematic so it stops moving.
- m_Rigidbody.isKinematic = true;
- }
-
-
- private void Start()
- {
- m_MovementAxisName = "Vertical";
- m_TurnAxisName = "Horizontal";
- }
-
-
- private void Update()
- {
- // Store the value of both input axes.
- m_MovementInputValue = Input.GetAxis(m_MovementAxisName);
- m_TurnInputValue = Input.GetAxis(m_TurnAxisName);
- }
-
-
- private void FixedUpdate()
- {
- // Adjust the rigidbodies position and orientation in FixedUpdate.
- Move();
- Turn();
- }
-
- public void Move(float directionValue)
- {
- // Create a vector in the direction the cozmo is facing with a magnitude based on the input, speed and the time between frames.
- Vector3 movement = directionValue * transform.forward * m_Speed * Time.deltaTime;
-
- // Apply this movement to the rigidbody's position.
- m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
- }
-
- public void Move()
- {
- Move(m_MovementInputValue);
- }
-
- //public void Move(float[] act)
- //{
-
- //}
-
- public void Turn()
- {
- Turn(m_TurnInputValue);
- }
-
- public void Turn(float turnValue)
- {
- // Determine the number of degrees to be turned based on the input, speed and time between frames.
- 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.
- m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);
- }
-
- }
|