MIF_E31221357/Assets/Scripts/JoystickMove.cs

41 lines
1.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JoystickMove : MonoBehaviour
{
public Joystick movementJoystick;
public float playerSpeed;
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
Transform startNode = GameObject.Find("nodes_0_0")?.transform;
if (startNode != null)
{
transform.position = startNode.position + new Vector3(0, 0, -1); // Pastikan tetap terlihat di depan
}
}
void Update()
{
if (movementJoystick.Direction.y !=0)
{
rb.linearVelocity = new Vector2(movementJoystick.Direction.x * playerSpeed, movementJoystick.Direction.y * playerSpeed);
} else
{
rb.linearVelocity = Vector2.zero;
}
Vector3 moveVector = (Vector3.right * movementJoystick.Horizontal + Vector3.up * movementJoystick.Vertical);
if (movementJoystick.Horizontal != 0 || movementJoystick.Vertical != 0)
{
transform.rotation = Quaternion.LookRotation(Vector3.forward, moveVector);
}
}
}