조이스틱 동작이 자연스럽게 구현하도록 수정 및 에디터 상에서 좌우 입력과 점프 동작 추가 적용함

This commit is contained in:
aube.lee
2025-01-28 18:45:33 +09:00
parent 752d02324a
commit e04ef0c613
11 changed files with 367 additions and 207 deletions

View File

@@ -6,39 +6,72 @@ namespace TON
{
public class CharacterBase : MonoBehaviour
{
public float speed;
public VariableJoystick joystick;
public float jumpForce = 10f; // 점프 힘
private bool isGrounded = true; // 플레이어가 바닥에 있는지 여부를 판단
private VariableJoystick joystick;
public Rigidbody2D rb;
public void Start()
{
joystick = ControllerUI.Instance.joystick;
ControllerUI.Instance.linkedCharactor = this;
}
public void FixedUpdate()
{
if (joystick != null)
// 키보드 입력과 조이스틱 입력 통합
float horizontalInput = Input.GetAxis("Horizontal"); // 키보드 좌우 입력
if (joystick != null && Mathf.Abs(joystick.Horizontal) > 0.01f)
{
Debug.Log($"Horizontal Input: {joystick.input.x}, {joystick.input.y}");
Debug.Log($"Horizontal Horizontal: {joystick.Horizontal}");
Debug.Log($"Horizontal Direction: {joystick.Direction}");
horizontalInput = joystick.Horizontal; // 조이스틱 입력 우선
}
// 조이스틱의 Horizontal 값 가져오기
float horizontalInput = joystick.Horizontal;
// 좌우 이동 처리 (X축 속도 설정)
float newVelocityX = horizontalInput * speed;
// 입력값이 0이 아닐 때만 처리
if (Mathf.Abs(horizontalInput) > 0.0f)
{
// 현재 위치 가져오기
Vector2 currentPosition = rb.position;
// Rigidbody2D의 속도 업데이트 (X축은 입력값 기반, Y축은 중력/점프 유지)
rb.velocity = new Vector2(newVelocityX, rb.velocity.y);
// 새로운 X 위치 계산
float newXPosition = currentPosition.x + horizontalInput * speed * Time.fixedDeltaTime;
// 디버그용 출력
// Debug.Log($"Horizontal Input: {horizontalInput}, Velocity: {rb.velocity}");
}
// Rigidbody2D의 위치 업데이트
rb.MovePosition(new Vector2(newXPosition, currentPosition.y));
}
public void Jump()
{
// 바닥에 있을 때만 점프 가능
if (isGrounded)
{
Debug.Log("Character Jump");
// 점프: 기존 X축 속도 유지, Y축 속도를 점프 힘으로 설정
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
// 점프 상태로 설정
isGrounded = false;
}
}
// 바닥 충돌 감지 (2D Physics)
private void OnCollisionEnter2D(Collision2D collision)
{
// Ground 태그가 붙은 오브젝트와 충돌 시 바닥 상태로 전환
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
public void Attack()
{
Debug.Log("character Attack");
}
}
}

View File

@@ -6,9 +6,16 @@ namespace TON
{
public class UIBase : MonoBehaviour
{
public Object eventSystem;
public virtual void Show()
{
Instantiate(Resources.Load("EventSystem/Prefabs/TON.EventSystem"));
// eventSystem이 현재 씬에 존재하지 않는 경우
if (eventSystem == null)
{
// UI EventSystem 정상 동작 되도록 GameObject Load
eventSystem = Instantiate(Resources.Load("EventSystem/Prefabs/TON.EventSystem"));
}
gameObject.SetActive(true);
}

View File

@@ -5,6 +5,10 @@ using UnityEngine.EventSystems;
namespace TON
{
/// <summary>
/// Joystick Assets에서 가져온 조이스틱 기본 동작 스크립트
/// </summary>
public class Joystick : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
{
public float Horizontal { get { return (snapX) ? SnapFloat(input.x, AxisOptions.Horizontal) : input.x; } }

View File

@@ -5,6 +5,9 @@ using UnityEngine.EventSystems;
namespace TON
{
/// <summary>
/// Joystick Assets에서 가져온 조이스틱 초기화 스크립트
/// </summary>
public class VariableJoystick : Joystick
{
public float MoveThreshold { get { return moveThreshold; } set { moveThreshold = Mathf.Abs(value); } }

View File

@@ -9,10 +9,19 @@ namespace TON
{
public static ControllerUI Instance => UIManager.Singleton.GetUI<ControllerUI>(UIList.ControllerUI);
/// <summary> 조이스틱에서 컨트롤된 x 값을 Player에서 사용할 수 있도록 객체화 </summary>
public VariableJoystick joystick;
private void Start()
{
public CharacterBase linkedCharactor { get; set; }
public void OnClickJumpButton()
{
linkedCharactor.Jump();
}
public void OnClickAttackButton()
{
linkedCharactor.Attack();
}
}
}