조이스틱 동작이 자연스럽게 구현하도록 수정 및 에디터 상에서 좌우 입력과 점프 동작 추가 적용함
This commit is contained in:
@@ -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");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user