Skip to content

Instantly share code, notes, and snippets.

@doncabreraphone
Created February 12, 2021 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doncabreraphone/3e4ae9f3683b8b10e15e9856b444dcb0 to your computer and use it in GitHub Desktop.
Save doncabreraphone/3e4ae9f3683b8b10e15e9856b444dcb0 to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;
[Serializable]
public class MoveInputEvent : UnityEvent<float, float> { }
public class InputController : MonoBehaviour
{
Controls controls;
public MoveInputEvent moveInputEvent;
private void Awake()
{
controls = new Controls();
}
private void OnEnable()
{
controls.Player.Enable();
controls.Player.Move.performed += OnMovePerformed;
controls.Player.Move.canceled += OnMovePerformed;
}
private void OnMovePerformed(InputAction.CallbackContext context)
{
Vector2 moveInput = context.ReadValue<Vector2>();
moveInputEvent.Invoke(moveInput.y, moveInput.x);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5.0f;
public float turnSpeed = 50f;
float horizontal;
float vertical;
public void Update()
{
Vector3 moveDirection = Vector3.forward * vertical + Vector3.right * horizontal;
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
}
public void OnMoveInput(float vertical, float horizontal)
{
this.vertical = vertical;
this.horizontal = horizontal;
Debug.Log($"Player Controller: Move Input: {vertical}, {horizontal}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment