78 lines
2.2 KiB
C#
78 lines
2.2 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class PlayerMove : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
float speed;
|
|
float inputValue;
|
|
|
|
|
|
public float maxSpeed;
|
|
public float jumpPower;
|
|
Rigidbody2D rigidBody;
|
|
SpriteRenderer spriteRenderer;
|
|
|
|
// TODO : 걷기, 점프 등 애니메이션
|
|
Animator animator;
|
|
|
|
// 사용하는 컴포넌트들 초기화
|
|
void Awake()
|
|
{
|
|
rigidBody = GetComponent<Rigidbody2D>();
|
|
spriteRenderer = GetComponent<SpriteRenderer>();
|
|
animator = GetComponent<Animator>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// // 점프
|
|
// if (Input.GetButtonDown("Jump"))
|
|
// {
|
|
// rigidBody.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
|
|
// }
|
|
//
|
|
// // 키 입력 땔때 캐릭터 멈춤
|
|
// if (Input.GetButtonUp("Horizontal"))
|
|
// {
|
|
// rigidBody.linearVelocity = new Vector2(rigidBody.linearVelocity.normalized.x * 0.0000001f, rigidBody.linearVelocity.y);
|
|
// }
|
|
//
|
|
// // 캐릭터(Sprite)이동 방향 바라보도록 스프라이트 플립
|
|
// if (Input.GetButton("Horizontal"))
|
|
// {
|
|
// spriteRenderer.flipX = Input.GetAxisRaw("Horizontal") >= 1;
|
|
// }
|
|
}
|
|
|
|
void FixedUpdate()
|
|
{
|
|
// // 캐릭터 움직임 컨트롤
|
|
// float h = Input.GetAxisRaw("Horizontal");
|
|
// rigidBody.AddForce(Vector2.right * h, ForceMode2D.Impulse);
|
|
|
|
rigidBody.linearVelocityX = inputValue * speed;
|
|
|
|
//
|
|
// // 유니티6 부터 Velocity에서 LinearVelocity로 변경
|
|
// if (rigidBody.linearVelocity.x > maxSpeed) // 오른쪽 최대 속도
|
|
// {
|
|
// rigidBody.linearVelocity = new Vector2(maxSpeed, rigidBody.linearVelocity.y);
|
|
// }
|
|
// else if (rigidBody.linearVelocity.x < maxSpeed * (-1))
|
|
// {
|
|
// rigidBody.linearVelocity = new Vector2(maxSpeed * (-1), rigidBody.linearVelocity.y);
|
|
// }
|
|
}
|
|
|
|
private void OnMove(InputValue value)
|
|
{
|
|
inputValue = value.Get<Vector2>().x;
|
|
}
|
|
|
|
private void OnJump()
|
|
{
|
|
rigidBody.AddForceY(jumpPower, ForceMode2D.Impulse);
|
|
}
|
|
} |