151 lines
3.7 KiB
C#
151 lines
3.7 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.InputSystem;
|
||
using Sirenix.OdinInspector;
|
||
using DG.Tweening;
|
||
|
||
public class Player : PlayerControl
|
||
{
|
||
/// <summary>
|
||
/// 玩家刚体组件喵
|
||
/// </summary>
|
||
Rigidbody2D m_rigidbody;
|
||
|
||
/// <summary>
|
||
/// 脚底位置子物体
|
||
/// </summary>
|
||
[Header("脚底位置")] [FoldoutGroup("子物体")]
|
||
public Transform GroundCheck;
|
||
|
||
/// <summary>
|
||
/// 角色朝向(向右为1)
|
||
/// </summary>
|
||
[ReadOnly] [Header("朝向监测")]
|
||
public int isRight = 1;
|
||
|
||
/// <summary>
|
||
/// 角色是否落地
|
||
/// </summary>
|
||
[ReadOnly]
|
||
[Header("落地监测")]
|
||
public bool isGround = false;
|
||
|
||
|
||
|
||
[Header("行走速度")][FoldoutGroup("角色操作数据")]
|
||
public float speed;
|
||
|
||
[Header("跳跃力量")][FoldoutGroup("角色操作数据")]
|
||
public float jumpForce;
|
||
|
||
|
||
/// <summary>
|
||
/// 角色翻面时长
|
||
/// </summary>
|
||
[Header("角色翻面所需时长")]
|
||
public float flipDuration = 0.1f;
|
||
|
||
|
||
/// <summary>
|
||
/// 玩家初始化喵
|
||
/// </summary>
|
||
override protected void Start()
|
||
{
|
||
base.Start();
|
||
m_rigidbody = gameObject.GetComponent<Rigidbody2D>();
|
||
|
||
}
|
||
|
||
//UPDATE
|
||
void Update()
|
||
{
|
||
OnInputDetect();
|
||
Flip();
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 翻面函数
|
||
/// </summary>
|
||
void Flip()
|
||
{
|
||
int lastFrameDir = isRight;
|
||
if (inputDir * lastFrameDir < 0)
|
||
{ isRight *= -1;
|
||
//transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
|
||
//transform.DOScale(new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z),0.1f);
|
||
|
||
//分情况定值翻转 可以规避在旋转动画未结束时再次翻转的造成的起始角度不同从而影响目标角度误差的问题
|
||
switch (isRight) {
|
||
case 1:
|
||
transform.DORotate(new Vector3(0, 0, 0), flipDuration);
|
||
break;
|
||
case -1:
|
||
transform.DORotate(new Vector3(0,180,0), flipDuration);
|
||
break;
|
||
}
|
||
|
||
|
||
}
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检测是否停止摇杆输入
|
||
/// </summary>
|
||
void OnInputDetect() {
|
||
if (inputDir == 0)
|
||
Freeze();
|
||
}
|
||
/// <summary>
|
||
/// 强制静止
|
||
/// </summary>
|
||
void Freeze() {
|
||
m_rigidbody.velocity = new Vector2(0,m_rigidbody.velocity.y);
|
||
}
|
||
|
||
/// <param name="ctx"></param>
|
||
override protected void OnMove(InputAction.CallbackContext ctx)
|
||
{
|
||
base.OnMove(ctx);
|
||
m_rigidbody.velocity = new Vector2(inputDir*speed,m_rigidbody.velocity.y);
|
||
}
|
||
override protected void OnAtk() {
|
||
|
||
}
|
||
override protected void OnJump() {
|
||
if (isGround == true) {
|
||
isGround = false;
|
||
m_rigidbody.velocity = new Vector2(m_rigidbody.velocity.x, jumpForce);
|
||
}
|
||
|
||
}
|
||
override protected void OnInteract() {
|
||
|
||
}
|
||
|
||
private void OnCollisionEnter2D(Collision2D collision)//当有物体碰上
|
||
{
|
||
//创到地面时触发一次
|
||
if (collision.transform.CompareTag("Ground"))
|
||
{
|
||
//向脚底发射一条短射线
|
||
Ray2D ray = new Ray2D(
|
||
GroundCheck.position,
|
||
Vector2.down
|
||
);
|
||
Debug.DrawRay(ray.origin, ray.direction, Color.red, 10f);
|
||
//获取射线的碰撞结果
|
||
RaycastHit2D hit2D;
|
||
hit2D = Physics2D.Raycast(ray.origin, ray.direction, 0.001f);
|
||
//如果射线有结果并且射线创到的是地面,才表示着地了
|
||
if (hit2D && hit2D.collider.transform.CompareTag("Ground"))
|
||
{
|
||
isGround = true;
|
||
}
|
||
else Debug.Log("Not Ground");
|
||
}
|
||
}
|
||
}
|