Created
October 18, 2022 14:41
-
-
Save fallenblood7080/909cecc23cf3be4dad3d17b0b9754513 to your computer and use it in GitHub Desktop.
Unity New Input System Mobile Joystick Controls
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 UnityEngine; | |
using UnityEngine.InputSystem; | |
public class InputManager : MonoBehaviour | |
{ | |
public Vector2 MoveInput; | |
public void OnMove(InputValue input) => MoveInput = input.Get<Vector2>(); | |
} |
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; | |
public class Movement : MonoBehaviour | |
{ | |
private InputManager inputs; | |
private Rigidbody2D rb; | |
public float Speed; | |
Vector2 moveDir; | |
bool isMoving; | |
void Awake() | |
{ | |
inputs = GetComponent<InputManager>(); | |
rb = GetComponent<Rigidbody2D>(); | |
} | |
void Update() | |
{ | |
moveDir = inputs.MoveInput.normalized; | |
isMoving = Convert.ToBoolean(moveDir.magnitude); | |
Rotate(); | |
} | |
void FixedUpdate() | |
{ | |
if (rb != null) | |
{ | |
rb.MovePosition(rb.position + moveDir * Speed * Time.fixedDeltaTime); | |
} | |
} | |
void Rotate() | |
{ | |
if (isMoving) | |
{ | |
float angle = Mathf.Atan2(moveDir.y, moveDir.x) * Mathf.Rad2Deg - 90f; | |
transform.rotation = Quaternion.Euler(0, 0, angle); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment