Player控制脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody2D rb;
public float speed;
public float jumpForce;
[Header("Ground Check")]
public Transform checkTrans;
public LayerMask checkLayer;
public float radius;
[Header("States Check")]
public bool isGround;
public bool canJump = false;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
InputCheck();
}
void FixedUpdate()
{
Movement();
Jump();
PhysicsCheck();
}
private void Movement()
{
float horizontalInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(horizontalInput * speed, rb.velocity.y);
if (horizontalInput != 0){
int dir = horizontalInput > 0 ? 0 : -1;
transform.rotation = Quaternion.Euler(0, dir * 180, 0);
}
}
private void InputCheck()
{
if (Input.GetKeyDown(KeyCode.W)&&isGround){
canJump = true;
}
}
private void Jump()
{
if (canJump){
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
canJump = false;
}
}
private void PhysicsCheck()
{
isGround = Physics2D.OverlapCircle(checkTrans.position, radius, checkLayer);
if (isGround)
rb.gravityScale = 1;
else
rb.gravityScale = 4;
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(checkTrans.position, radius);
}
}