Skip to content

Instantly share code, notes, and snippets.

@fallenblood7080
Created October 18, 2022 14:41
Show Gist options
  • Save fallenblood7080/909cecc23cf3be4dad3d17b0b9754513 to your computer and use it in GitHub Desktop.
Save fallenblood7080/909cecc23cf3be4dad3d17b0b9754513 to your computer and use it in GitHub Desktop.
Unity New Input System Mobile Joystick Controls
using UnityEngine;
using UnityEngine.InputSystem;
public class InputManager : MonoBehaviour
{
public Vector2 MoveInput;
public void OnMove(InputValue input) => MoveInput = input.Get<Vector2>();
}
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