Skip to content

Instantly share code, notes, and snippets.

@alikrc
Last active September 13, 2020 19:09
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 alikrc/70ea9b570972ee7c77d7646d5b4ffcda to your computer and use it in GitHub Desktop.
Save alikrc/70ea9b570972ee7c77d7646d5b4ffcda to your computer and use it in GitHub Desktop.
New input system unity with send messages behaviour. Set a projectile prefab before running
using System;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
public float speed = 10.0f;
public GameObject projectilePrefab;
private InputAction moveAction;
private InputAction fireAction;
private Vector3 moveVector;
private void Awake()
{
var actions = GetComponent<PlayerInput>().actions;
moveAction = actions.FindAction("Move");
fireAction = actions.FindAction("Fire");
}
void Start()
{
moveVector = new Vector3();
}
void Move(Vector2 inputMoveVector)
{
Debug.Log(DateTime.Now.ToLocalTime());
moveVector.Set(inputMoveVector.x, 0, inputMoveVector.y);
transform.Translate(moveVector * speed * Time.deltaTime);
}
void OnFire()
{
Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);
}
void Update()
{
var inputMoveVector = moveAction.ReadValue<Vector2>();
if (inputMoveVector != Vector2.zero)
{
Move(inputMoveVector);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment