2DAdventureGit/Assets/Scripts/Player/PlayerController.cs
2025-02-20 21:24:00 +08:00

81 lines
1.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
public PlayerInputControl inputControl;
public Vector2 inputDirection;
private Rigidbody2D rb;
[Header("»ù±¾²ÎÊý")]
public float speed;
public float jumpForce;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
inputControl = new PlayerInputControl();
inputControl.GamePlayerInput.Jump.started += Jump;
}
private void OnEnable()
{
inputControl.Enable();
}
private void OnDisable()
{
inputControl.Disable();
}
void Start()
{
}
// Update is called once per frame
void Update()
{
inputDirection = inputControl.GamePlayerInput.Move.ReadValue<Vector2>();
}
private void FixedUpdate()
{
Move();
}
//ÒÆ¶¯
public void Move()
{
rb.velocity = new Vector2(inputDirection.x*speed * Time.deltaTime, rb.velocity.y);
//·­×ª
int faceDir = (int)transform.localScale.x;
if(inputDirection.x > 0 )
{
faceDir = 1;
}
if (inputDirection.x < 0)
{
faceDir = -1;
}
transform.localScale = new Vector3(faceDir, transform.localScale.y, transform.localScale.z);
}
//ÌøÔ¾
private void Jump(InputAction.CallbackContext context)
{
rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
}
}