Drive
Objetivo
Recebe o Input do teclado ou gamepad utilizando o new Input System para movimentação de simulação de veículo. Baseano no script Drive, do exemplo Input.GetAxis, da Unity e adaptado para o Input System novo.
Como usar
Adicione a um game object e, no Inspector, associe a referência Move Action Player/Move, que pode ser encontrado InputSystem_reference, que é criado por padrão em novos projetos Unity 6 (na pasta assets)
Script Drive.cs
using UnityEngine;
using UnityEngine.InputSystem;
public class Drive : MonoBehaviour
{
public float speed = 5.0f; // 5 metros por segundo
public float rotationSpeed = 100.0f; // 100 graus por segundo
public bool invertRotationWhenBackwards = true;
[SerializeField] InputActionReference moveAction;
private void OnEnable() { if (moveAction != null) moveAction.action.Enable(); }
private void OnDisable() { if (moveAction != null) moveAction.action.Disable(); }
void Update()
{
if (moveAction == null) return;
Vector2 moveInput = moveAction.action.ReadValue<Vector2>();
if(invertRotationWhenBackwards)
moveInput.x = moveInput.y < 0 ? -moveInput.x : moveInput.x;
Vector3 newDirection = new Vector3(0f, 0f, moveInput.y).normalized;
Vector3 newRotation = new Vector3(0f, moveInput.x, 0f).normalized;
transform.Translate(newDirection * speed * Time.deltaTime);
transform.Rotate(newRotation * rotationSpeed * Time.deltaTime);
}
}