level 4
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
public PlayerInputControl inputControl;
public Vector2 inputDirection;
public float speed;
private Rigidbody2D rb;
private void Awake()
{
inputControl = new PlayerInputControl(); // 注意:这里可能不是最佳实践,通常Unity的Input System是通过Inspector赋值
rb = GetComponent<Rigidbody2D>();
inputDirection = Vector2.zero; // 初始化inputDirection
rb = GetComponent<Rigidbody2D>();
}
private void OnEnable()
{
inputControl.Enable();
}
private void OnDisable()
{
inputControl.Disable();
}
private void Update()
{
inputDirection = inputControl.Gameplay.Move.ReadValue<Vector2>();
}
private void FixedUpdate()
{
Move();
}
public void Move()
{
网页链接 = new Vector2(speed * inputDirection.x * 网页链接 , rb.velocity.y);
int faceDir = (int)transform.localScale.x;
if (inputDirection.x > 0)
faceDir = 1;
if (inputDirection.x < 0)
faceDir = -1;
网页链接 = new Vector3(faceDir, 1, 1);
}
}
2024年08月28日 15点08分

