Last active
September 13, 2020 19:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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