반응형
- 코드 작성
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float jumpForce = 10f;
public float moveSpeed = 5f;
public float groundCheckRadius = 0.2f;
public LayerMask whatIsGround;
public Transform groundCheck;
private Rigidbody2D rb;
private bool isGrounded = false;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
float horizontalInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(horizontalInput * moveSpeed, rb.velocity.y);
if (isGrounded && Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
}
반응형