조이스틱으로 player 오브젝트 좌우 이동 로직 적용

This commit is contained in:
aube.lee
2025-01-24 19:03:32 +09:00
parent 10f31b0222
commit 752d02324a
11 changed files with 397 additions and 182 deletions

View File

@@ -0,0 +1,44 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace TON
{
public class CharacterBase : MonoBehaviour
{
public float speed;
public VariableJoystick joystick;
public Rigidbody2D rb;
public void Start()
{
joystick = ControllerUI.Instance.joystick;
}
public void FixedUpdate()
{
if (joystick != null)
{
Debug.Log($"Horizontal Input: {joystick.input.x}, {joystick.input.y}");
Debug.Log($"Horizontal Horizontal: {joystick.Horizontal}");
Debug.Log($"Horizontal Direction: {joystick.Direction}");
// 조이스틱의 Horizontal 값 가져오기
float horizontalInput = joystick.Horizontal;
// 입력값이 0이 아닐 때만 처리
if (Mathf.Abs(horizontalInput) > 0.0f)
{
// 현재 위치 가져오기
Vector2 currentPosition = rb.position;
// 새로운 X 위치 계산
float newXPosition = currentPosition.x + horizontalInput * speed * Time.fixedDeltaTime;
// Rigidbody2D의 위치 업데이트
rb.MovePosition(new Vector2(newXPosition, currentPosition.y));
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 163a5df0eeadca842adba0310b08b93e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -6,6 +6,14 @@ namespace TON
{
public class CharacterController : MonoBehaviour
{
private CharacterBase linkedCharactor;
public LayerMask groundLayer;
private void Awake()
{
linkedCharactor = GetComponent<CharacterBase>();
}
}
}