118 lines
4.0 KiB
C#
118 lines
4.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
|
|
//这个是翻越,懒得改名字了
|
|
public class Skill_Jump : MonoBehaviour
|
|
{
|
|
public CharacterAin CharacterAin; // 用于控制翻越动画
|
|
public float vaultDistance = 4f; // 角色可以翻越的最大距离
|
|
public float vaultHeight = 4f; // 角色可以翻越的最大高度
|
|
public LayerMask vaultLayerMask; // 用于检测可翻越物体的 Layer
|
|
public float vaultForwardDistance = 0.5f; // 可调整的翻越前进距离
|
|
public float vaultUpwardHeight = 1.0f; // 可调整的翻越高度
|
|
|
|
private bool isVaulting = false; // 标志角色是否正在翻越
|
|
private Collider characterCollider; // 角色的碰撞体
|
|
public Rigidbody characterRigidbody; // 角色的刚体
|
|
|
|
private void Start()
|
|
{
|
|
CharacterAin = transform.Find("newPlayer").GetComponent<CharacterAin>();
|
|
characterCollider = GetComponent<Collider>();
|
|
characterRigidbody = GetComponent<Rigidbody>();
|
|
}
|
|
void Update()
|
|
{
|
|
//
|
|
if (Input.GetKeyDown(KeyCode.Space) && !isVaulting)
|
|
{
|
|
Debug.Log("按下翻越");
|
|
CheckForVault();
|
|
}
|
|
}
|
|
|
|
// 检测是否可以翻越
|
|
private void CheckForVault()
|
|
{
|
|
Debug.Log("正在检测");
|
|
RaycastHit hit;
|
|
|
|
Debug.DrawRay(transform.position, transform.forward * vaultDistance, Color.red, 2f);
|
|
|
|
Vector3 rayStartPosition = transform.position + Vector3.up * 0.9f;
|
|
Debug.DrawRay(rayStartPosition, transform.forward * vaultDistance, Color.red, 2f);
|
|
Physics.Raycast(transform.position, transform.forward, out hit, vaultDistance, vaultLayerMask);
|
|
|
|
Debug.Log(hit.transform.name);
|
|
// 从角色前方向前发射一个射线,检测是否有障碍物
|
|
if (Physics.Raycast(transform.position, transform.forward, out hit, vaultDistance, vaultLayerMask))
|
|
{
|
|
Debug.Log("============================"+hit);
|
|
// 如果检测到的障碍物高度适合翻越
|
|
if (hit.collider.bounds.size.y <= vaultHeight)
|
|
{
|
|
Debug.Log("检测可以翻越");
|
|
StartVault(hit.point);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 开始翻越动作
|
|
private void StartVault(Vector3 hitPoint)
|
|
{
|
|
Debug.Log("翻越");
|
|
isVaulting = true;
|
|
CharacterAin.SetSkillAin(CharacterSkill.jump);
|
|
|
|
// 禁用碰撞体和刚体,防止翻越时受到物理影响
|
|
characterCollider.enabled = false;
|
|
characterRigidbody.isKinematic = true;
|
|
|
|
// 禁用角色的物理移动,防止翻越时移动受影响
|
|
GetComponent<CharacterControl>().enabled = false;
|
|
|
|
// 让角色移动到翻越后的位置
|
|
StartCoroutine(VaultMovement(hitPoint));
|
|
}
|
|
|
|
// 控制角色翻越过程中的移动
|
|
private IEnumerator VaultMovement(Vector3 hitPoint)
|
|
{
|
|
// 忽略 hitPoint 的 Y 轴,确保角色平行移动
|
|
Vector3 horizontalHitPoint = new Vector3(hitPoint.x, transform.position.y, hitPoint.z);
|
|
|
|
// 调整翻越的位移和高度
|
|
Vector3 targetPosition = horizontalHitPoint + transform.forward * vaultForwardDistance;
|
|
targetPosition.y += vaultUpwardHeight; // 在目标位置上加上高度调整
|
|
|
|
float vaultTime = 1.0f; // 翻越所需的时间
|
|
float elapsedTime = 0f;
|
|
|
|
Vector3 startPosition = transform.position;
|
|
|
|
while (elapsedTime < vaultTime)
|
|
{
|
|
transform.position = Vector3.Lerp(startPosition, targetPosition, elapsedTime / vaultTime); // 平滑移动角色
|
|
elapsedTime += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
transform.position = targetPosition; // 确保角色到达目标位置
|
|
|
|
// 立即恢复物理控制和碰撞体
|
|
characterRigidbody.isKinematic = false;
|
|
characterCollider.enabled = true;
|
|
GetComponent<CharacterControl>().enabled = true;
|
|
|
|
// 确保刚体恢复正常的重力影响
|
|
characterRigidbody.velocity = Vector3.zero; // 清除任何残留的速度
|
|
characterRigidbody.AddForce(Vector3.down * 9.81f, ForceMode.VelocityChange); // 施加重力
|
|
|
|
CharacterAin.SetSkillAin(CharacterSkill.NoSkill);
|
|
isVaulting = false; // 结束翻越动作
|
|
Debug.Log("翻越结束");
|
|
}
|
|
}
|